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.
This commit is contained in:
Alan Agius 2025-03-11 13:05:00 +00:00
parent 5ff4c28e7b
commit 5bea3de4cb

View File

@ -6,18 +6,26 @@
* found in the LICENSE file at https://angular.dev/license * found in the LICENSE file at https://angular.dev/license
*/ */
import assert from 'node:assert';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import type { Connect } from 'vite'; import type { Connect } from 'vite';
type DevToolsJson = {
workspace: {
root: string;
uuid: string;
};
};
const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json'; const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json';
export function createChromeDevtoolsMiddleware( export function createChromeDevtoolsMiddleware(
cacheDir: string, cacheDir: string,
projectRoot: string, projectRoot: string,
): Connect.NextHandleFunction { ): Connect.NextHandleFunction {
let devtoolsConfig: string; let devtoolsConfig: string | undefined;
const devtoolsConfigPath = join(cacheDir, 'com.chrome.devtools.json'); const devtoolsConfigPath = join(cacheDir, 'com.chrome.devtools.json');
return function chromeDevtoolsMiddleware(req, res, next) { return function chromeDevtoolsMiddleware(req, res, next) {
@ -27,22 +35,27 @@ export function createChromeDevtoolsMiddleware(
return; return;
} }
// We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server. if (!devtoolsConfig) {
try { // We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
devtoolsConfig ??= readFileSync(devtoolsConfigPath, 'utf-8');
} catch {
const devtoolsConfigJson = {
workspace: {
root: projectRoot,
uuid: randomUUID(),
},
};
devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
try { try {
mkdirSync(cacheDir, { recursive: true }); const devtoolsConfig = readFileSync(devtoolsConfigPath, 'utf-8');
writeFileSync(devtoolsConfigPath, devtoolsConfig); const devtoolsConfigJson: DevToolsJson = JSON.parse(devtoolsConfig);
} catch {} assert.equal(projectRoot, devtoolsConfigJson?.workspace.root);
} catch {
const devtoolsConfigJson: DevToolsJson = {
workspace: {
root: projectRoot,
uuid: randomUUID(),
},
};
devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
try {
mkdirSync(cacheDir, { recursive: true });
writeFileSync(devtoolsConfigPath, devtoolsConfig);
} catch {}
}
} }
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');