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

Refactor config loading #7622

Merged
merged 6 commits into from
Jul 12, 2023
Merged

Refactor config loading #7622

merged 6 commits into from
Jul 12, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Jul 11, 2023

Changes

  • Simplify config loading via nodejs and vite
  • Move config merging logic to separate file
  • Bug fix with telemetry report on config error

Testing

Tested manually for config loading and restarting logic. The smoke tests should also cover this.

Docs

n/a. internal refactor.

@changeset-bot
Copy link

changeset-bot bot commented Jul 11, 2023

⚠️ No Changeset found

Latest commit: 99b68a8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jul 11, 2023
@@ -102,13 +102,10 @@ async function handleConfigError(
error(logging, 'astro', `Unable to load ${path ? colors.bold(path) : 'your Astro config'}\n`);
if (e instanceof ZodError) {
console.error(formatConfigErrorMessage(e) + '\n');
telemetry.record(eventConfigError({ cmd, err: e, isFatal: true }));
Copy link
Member Author

@bluwy bluwy Jul 11, 2023

Choose a reason for hiding this comment

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

eventConfigError only accepts ZodError. This was added in #7316

Comment on lines -250 to -256
const tempConfigPath = path.join(
root,
`.temp.${Date.now()}.config${path.extname(configPath)}`
);

const currentConfigContent = await fsMod.promises.readFile(configPath, 'utf-8');
await fs.promises.writeFile(tempConfigPath, currentConfigContent);
Copy link
Member Author

@bluwy bluwy Jul 11, 2023

Choose a reason for hiding this comment

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

This custom write flow is converted to

await import(pathToFileURL(configPath).toString() + '?t=' + Date.now());

in vite-load.ts. This was initially added in #4578 for proload support which we now don't use.

Comment on lines 233 to 236
if (!configPath) return undefined;

// Create a vite server to load the config
return await loadConfigWithVite({
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we have the configPath guard here, we can refactor loadConfigWithVite to return the config directly, instead of { value, configPath }, because we already know the configPath.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Let's 🚢 it!

Comment on lines 223 to 226
async function tryLoadConfig(
configOptions: LoadConfigOptions,
root: string
): Promise<TryLoadConfigResult | undefined> {
): Promise<Record<string, any> | undefined> {
Copy link
Member

@ematipico ematipico Jul 11, 2023

Choose a reason for hiding this comment

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

While we're refactoring, I want to propose the following change, with two options:

  1. rename tryLoadConfig to loadConfig and always return a Record<string, any>. The function will return {} if no configPath is found;
  2. keep the name tryLoadConfig but throw a runtime error, and let the consumer handle the error;

What do you think?

It's a pity that there's no way to signal that a function can throw an error to a consumer :(

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call! I didn't spot the loadConfig opportunity (option 1) which we can definitely do. Went with that instead since throwing errors are a bit expensive.

@bluwy bluwy merged commit 0952a81 into main Jul 12, 2023
@bluwy bluwy deleted the config-loading-change branch July 12, 2023 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants