From 308b5d8c122f44e7724bb2f3ad3aa5c43a83e584 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 12 Apr 2024 13:56:54 -0400 Subject: [PATCH] fix(add): Fixes astro add modifying baseUrl by accident (#10774) * fix(add): Fixes `astro add` modifying `baseUrl` by accident * chore: changeset * test: add test * fix: tsconfig not being a json maybe is a mistake, I don't know! * test: fix * Update packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json Co-authored-by: Nate Moore --------- Co-authored-by: Nate Moore --- .changeset/mean-candles-hammer.md | 5 +++++ packages/astro/src/cli/add/index.ts | 6 +++--- packages/astro/src/core/config/tsconfig.ts | 18 +++++++++++++++--- .../tsconfig-handling/baseUrl/tsconfig.json | 6 ++++++ .../test/units/config/config-tsconfig.test.js | 10 ++++++++++ 5 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 .changeset/mean-candles-hammer.md create mode 100644 packages/astro/test/fixtures/tsconfig-handling/baseUrl/tsconfig.json diff --git a/.changeset/mean-candles-hammer.md b/.changeset/mean-candles-hammer.md new file mode 100644 index 0000000000..cb825a493a --- /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 d1c7eec2e9..2f8b73d560 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 f0e7478778..f0236309e4 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 0000000000..bd5b17453a --- /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 c6372cbe07..f83e9bcc30 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', () => {