Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(add): Fixes astro add modifying baseUrl by accident #10774

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +90 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

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
Loading