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 inline root resolve logic #7977

Merged
merged 2 commits into from
Aug 7, 2023
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/quick-eagles-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix inline root resolve logic
15 changes: 15 additions & 0 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/units/config/config-resolve.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});