Skip to content
Open
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
49 changes: 47 additions & 2 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,16 @@ export default async function postInstall(options: PostinstallOptions) {
);
}

const getTemplateName = () => {
const getTemplateName = (configContent?: string) => {
if (isVitest4OrNewer) {
return 'vitest.config.4.template';
} else if (isVitest3_2To4) {
// In Vitest 3.2, `workspace` was deprecated in favor of `projects` but still works.
// If the user's existing config already uses `workspace`, use the old template that
// also uses `workspace` so that the merge doesn't introduce both keys.
if (configContent && configUsesWorkspace(configContent)) {
return 'vitest.config.template';
}
return 'vitest.config.3.2.template';
}
return 'vitest.config.template';
Comment on lines +193 to 205
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Resolve the exported config before picking the workspace vs. projects template.

configUsesWorkspace() does a file-wide ObjectProperty scan and only recognizes inline test: { workspace: ... }. That misses shapes like const test = { workspace: [...] }; export default defineConfig({ test }), and it can also false-positive on unrelated test.workspace objects elsewhere in the file. In either case getTemplateName(configFile) can choose the wrong template and reintroduce the workspace/projects conflict this change is trying to avoid. Reusing the same export-resolution path that updateConfigFile() uses would make template selection match the config shapes this PR now supports.

Also applies to: 264-264, 451-484

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/addons/vitest/src/postinstall.ts` around lines 193 - 205, The
getTemplateName function currently calls configUsesWorkspace(configContent) on
the raw file text which misses exported/aliased test objects and can
false-positive; change getTemplateName to first resolve the exported config
AST/value using the same export-resolution logic used by updateConfigFile (reuse
or call that resolver) and pass the resolved config object or its serialized
source to configUsesWorkspace (or a new helper that inspects the resolved test
property) so template selection correctly detects whether the effective config
uses workspace vs projects before returning 'vitest.config.template' vs
'vitest.config.3.2.template' (keep references to getTemplateName,
configUsesWorkspace, and updateConfigFile to locate the code).

Expand Down Expand Up @@ -255,7 +261,7 @@ export default async function postInstall(options: PostinstallOptions) {
/\/\/\/\s*<reference\s+types=["']vitest\/config["']\s*\/>/
);

const templateName = getTemplateName();
const templateName = getTemplateName(configFile);

const alreadyConfigured = isConfigAlreadySetup(rootConfig, configFile);

Expand Down Expand Up @@ -437,3 +443,42 @@ export function isConfigAlreadySetup(_configPath: string, configContent: string)

return pluginReferenced;
}

/**
* Checks whether an existing config file uses `test.workspace` (Vitest 3.0-3.1 style) rather than
* `test.projects` (Vitest 3.2+ style).
*/
function configUsesWorkspace(configContent: string): boolean {
let ast: ReturnType<typeof babelParse>;
try {
ast = babelParse(configContent);
} catch {
return false;
}

let found = false;

traverse(ast, {
ObjectProperty(path) {
if (found) {
path.stop();
return;
}
const key = path.node.key;
if (key.type === 'Identifier' && key.name === 'workspace') {
// Check that this is inside a `test` property to avoid false positives
const parent = path.parentPath?.parentPath;
if (
parent?.isObjectProperty() &&
parent.node.key.type === 'Identifier' &&
parent.node.key.name === 'test'
) {
found = true;
path.stop();
}
}
},
});

return found;
}
Loading
Loading