Skip to content

Commit 5bea3de

Browse files
committedMar 11, 2025
fix(@angular/build): invalidate com.chrome.devtools.json if project is moved
Ensure that when a project is relocated, the `com.chrome.devtools.json` file is properly invalidated by checking the `root` path.
1 parent 5ff4c28 commit 5bea3de

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed
 

Diff for: ‎packages/angular/build/src/tools/vite/middlewares/chrome-devtools-middleware.ts

+29-16
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import assert from 'node:assert';
910
import { randomUUID } from 'node:crypto';
1011
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
1112
import { join } from 'node:path';
1213
import type { Connect } from 'vite';
1314

15+
type DevToolsJson = {
16+
workspace: {
17+
root: string;
18+
uuid: string;
19+
};
20+
};
21+
1422
const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json';
1523

1624
export function createChromeDevtoolsMiddleware(
1725
cacheDir: string,
1826
projectRoot: string,
1927
): Connect.NextHandleFunction {
20-
let devtoolsConfig: string;
28+
let devtoolsConfig: string | undefined;
2129
const devtoolsConfigPath = join(cacheDir, 'com.chrome.devtools.json');
2230

2331
return function chromeDevtoolsMiddleware(req, res, next) {
@@ -27,22 +35,27 @@ export function createChromeDevtoolsMiddleware(
2735
return;
2836
}
2937

30-
// We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
31-
try {
32-
devtoolsConfig ??= readFileSync(devtoolsConfigPath, 'utf-8');
33-
} catch {
34-
const devtoolsConfigJson = {
35-
workspace: {
36-
root: projectRoot,
37-
uuid: randomUUID(),
38-
},
39-
};
40-
41-
devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
38+
if (!devtoolsConfig) {
39+
// We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
4240
try {
43-
mkdirSync(cacheDir, { recursive: true });
44-
writeFileSync(devtoolsConfigPath, devtoolsConfig);
45-
} catch {}
41+
const devtoolsConfig = readFileSync(devtoolsConfigPath, 'utf-8');
42+
const devtoolsConfigJson: DevToolsJson = JSON.parse(devtoolsConfig);
43+
assert.equal(projectRoot, devtoolsConfigJson?.workspace.root);
44+
} catch {
45+
const devtoolsConfigJson: DevToolsJson = {
46+
workspace: {
47+
root: projectRoot,
48+
uuid: randomUUID(),
49+
},
50+
};
51+
52+
devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
53+
54+
try {
55+
mkdirSync(cacheDir, { recursive: true });
56+
writeFileSync(devtoolsConfigPath, devtoolsConfig);
57+
} catch {}
58+
}
4659
}
4760

4861
res.setHeader('Content-Type', 'application/json');

0 commit comments

Comments
 (0)