Skip to content

Commit

Permalink
fix(add): Fixes astro add modifying baseUrl by accident (withastro#10774
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>

---------

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
2 people authored and PeterDraex committed Apr 23, 2024
1 parent 197d67e commit 808c994
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-candles-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes `astro add` sometimes modifying `baseUrl` unintentionally
6 changes: 3 additions & 3 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
18 changes: 15 additions & 3 deletions packages/astro/src/core/config/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -64,7 +66,7 @@ type TSConfigResult<T = {}> = Promise<
export async function loadTSConfig(
root: string | undefined,
findUp = false
): Promise<TSConfigResult<{ rawConfig: TSConfckParseResult }>> {
): Promise<TSConfigResult<{ rawConfig: TSConfig }>> {
const safeCwd = root ?? process.cwd();

const [jsconfig, tsconfig] = await Promise.all(
Expand 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) {
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files": ["i-have-base-url"],
"compilerOptions": {
"baseUrl": ".",
}
}
10 changes: 10 additions & 0 deletions packages/astro/test/units/config/config-tsconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 808c994

Please sign in to comment.