Fix inline root resolve logic (#7977)

This commit is contained in:
Bjorn Lu 2023-08-07 21:43:03 +08:00 committed by GitHub
parent 51028f85c6
commit a4a637c8f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix inline root resolve logic

View file

@ -211,6 +211,10 @@ async function loadConfig(
}
}
/**
* `AstroInlineConfig` is a union of `AstroUserConfig` and `AstroInlineOnlyConfig`.
* This functions splits it up.
*/
function splitInlineConfig(inlineConfig: AstroInlineConfig): {
inlineUserConfig: AstroUserConfig;
inlineOnlyConfig: AstroInlineOnlyConfig;
@ -231,6 +235,12 @@ interface ResolveConfigResult {
astroConfig: AstroConfig;
}
/**
* Resolves the Astro config with a given inline config.
*
* @param inlineConfig An inline config that takes highest priority when merging and resolving the final config.
* @param command The running command that uses this config. Usually 'dev' or 'build'.
*/
export async function resolveConfig(
inlineConfig: AstroInlineConfig,
command: string,
@ -239,6 +249,11 @@ export async function resolveConfig(
const root = resolveRoot(inlineConfig.root);
const { inlineUserConfig, inlineOnlyConfig } = splitInlineConfig(inlineConfig);
// If the root is specified, assign the resolved path so it takes the highest priority
if (inlineConfig.root) {
inlineUserConfig.root = root;
}
const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod);
const mergedConfig = mergeConfig(userConfig, inlineUserConfig);
const astroConfig = await validateConfig(mergedConfig, root, command);

View file

@ -0,0 +1,18 @@
import { expect } from 'chai';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolveConfig } from '../../../dist/core/config/index.js';
describe('resolveConfig', () => {
it('resolves relative inline root correctly', async () => {
const { astroConfig } = await resolveConfig(
{
configFile: false,
root: 'relative/path',
},
'dev'
);
const expectedRoot = path.join(process.cwd(), 'relative/path/');
expect(fileURLToPath(astroConfig.root)).to.equal(expectedRoot);
});
});