diff --git a/.changeset/mean-candles-hammer.md b/.changeset/mean-candles-hammer.md new file mode 100644 index 000000000000..cb825a493a58 --- /dev/null +++ b/.changeset/mean-candles-hammer.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes `astro add` sometimes modifying `baseUrl` unintentionally diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index d1c7eec2e9ca..2f8b73d560f1 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -968,16 +968,16 @@ async function updateTSConfig( inputConfig = { tsconfig: defaultTSConfig, tsconfigFile: path.join(cwd, 'tsconfig.json'), - rawConfig: { tsconfig: defaultTSConfig, tsconfigFile: path.join(cwd, 'tsconfig.json') }, + rawConfig: defaultTSConfig, }; } else { - inputConfigText = JSON.stringify(inputConfig.rawConfig.tsconfig, null, 2); + inputConfigText = JSON.stringify(inputConfig.rawConfig, null, 2); } const configFileName = path.basename(inputConfig.tsconfigFile); const outputConfig = updateTSConfigForFramework( - inputConfig.rawConfig.tsconfig, + inputConfig.rawConfig, firstIntegrationWithTSSettings ); diff --git a/packages/astro/src/core/config/tsconfig.ts b/packages/astro/src/core/config/tsconfig.ts index f0e747877842..f0236309e492 100644 --- a/packages/astro/src/core/config/tsconfig.ts +++ b/packages/astro/src/core/config/tsconfig.ts @@ -1,10 +1,12 @@ import { join } from 'node:path'; +import { readFile } from "node:fs/promises" import { TSConfckParseError, type TSConfckParseOptions, type TSConfckParseResult, find, parse, + toJson } from 'tsconfck'; import type { CompilerOptions, TypeAcquisition } from 'typescript'; @@ -64,7 +66,7 @@ type TSConfigResult = Promise< export async function loadTSConfig( root: string | undefined, findUp = false -): Promise> { +): Promise> { const safeCwd = root ?? process.cwd(); const [jsconfig, tsconfig] = await Promise.all( @@ -85,7 +87,13 @@ export async function loadTSConfig( return parsedConfig; } - return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig }; + // tsconfck does not return the original config, so we need to parse it ourselves + // https://github.com/dominikg/tsconfck/issues/138 + const rawConfig = await readFile(tsconfig, 'utf-8') + .then(toJson) + .then((content) => JSON.parse(content) as TSConfig); + + return { ...parsedConfig, rawConfig }; } if (jsconfig) { @@ -95,7 +103,11 @@ export async function loadTSConfig( return parsedConfig; } - return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig }; + const rawConfig = await readFile(jsconfig, 'utf-8') + .then(toJson) + .then((content) => JSON.parse(content) as TSConfig); + + return { ...parsedConfig, rawConfig: rawConfig }; } return 'missing-config'; diff --git a/packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json b/packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json new file mode 100644 index 000000000000..bd5b17453a2c --- /dev/null +++ b/packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json @@ -0,0 +1,6 @@ +{ + "files": ["i-have-base-url"], + "compilerOptions": { + "baseUrl": ".", + } +} diff --git a/packages/astro/test/units/config/config-tsconfig.test.js b/packages/astro/test/units/config/config-tsconfig.test.js index c6372cbe0766..f83e9bcc3036 100644 --- a/packages/astro/test/units/config/config-tsconfig.test.js +++ b/packages/astro/test/units/config/config-tsconfig.test.js @@ -3,6 +3,8 @@ import * as path from 'node:path'; import { describe, it } from 'node:test'; import { fileURLToPath } from 'node:url'; import { loadTSConfig, updateTSConfigForFramework } from '../../../dist/core/config/index.js'; +import { readFile } from 'node:fs/promises'; +import { toJson } from 'tsconfck'; const cwd = fileURLToPath(new URL('../../fixtures/tsconfig-handling/', import.meta.url)); @@ -37,6 +39,14 @@ describe('TSConfig handling', () => { assert.equal(invalidConfig, 'invalid-config'); assert.equal(missingConfig, 'missing-config'); }); + + it('does not change baseUrl in raw config', async () => { + const loadedConfig = await loadTSConfig(path.join(cwd, 'baseUrl')); + const rawConfig = await readFile(path.join(cwd, 'baseUrl', 'tsconfig.json'), 'utf-8').then(toJson) + .then((content) => JSON.parse(content)); + + assert.deepEqual(loadedConfig.rawConfig, rawConfig); + }); }); describe('tsconfig / jsconfig updates', () => {