Skip to content

fix: use Vite root config setting instead of current working directory#15469

Open
teemingc wants to merge 28 commits intoversion-3from
fix-vite-workspaces
Open

fix: use Vite root config setting instead of current working directory#15469
teemingc wants to merge 28 commits intoversion-3from
fix-vite-workspaces

Conversation

@teemingc
Copy link
Member

@teemingc teemingc commented Mar 2, 2026

closes #11356
closes #12140
closes #15188

This PR standardises the use of process.cwd() to prefer the Vite config root setting when available similar to VPS. This helps us resolve paths correctly before reading files or generating tsconfig paths. It eases lots of monorepo tooling usage, particularly Vitest workspaces. This also allows us to use the Vitest IDE extension to run unit tests (which didn't work before because of the aforementioned issues).

We could backport these changes to v2 but it's a bit easier to base it on v3 which already makes use of import.meta.dirname in many places thanks to #15434

I've only made changes to kit itself for now, so other packages still default to process.cwd() but those are probably less problematic since they won't run for Vitest tests


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 2, 2026

⚠️ No Changeset found

Latest commit: 79057c6

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

@teemingc teemingc changed the base branch from main to version-3 March 2, 2026 21:01
@svelte-docs-bot
Copy link

*/
export function get_page_options(filepath) {
export function get_page_options(filepath, root) {
const input = read(path.resolve(root, filepath));
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this out of the try...catch to avoid silent errors for files it can't find when calling read. The real issue was that the route filepaths weren't being resolved correctly. That's not an issue anymore now that we resolve against the root.

id: '/blog.json',
pattern: '/^/blog.json/?$/',
endpoint: { file: 'samples/basic/blog.json/+server.js', page_options: null }
endpoint: { file: 'samples/basic/blog.json/+server.js', page_options: {} }
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixes the test results to match the change from https://github.com/sveltejs/kit/pull/15469/changes#diff-8701fabc771d319105f7401163b7bc28189c5a11e30706215a62fb303385797a . Previously, the page options were incorrectly computed as null (unable to statically analyse). However, the correct value is {} (which means no page options exported).

@teemingc teemingc marked this pull request as ready for review March 4, 2026 13:22

fs.writeFileSync(
'./src/version.js',
path.join(import.meta.dirname, '..', 'src', 'version.js'),
Copy link
Member Author

Choose a reason for hiding this comment

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

Helps the version generation test run from the Vitest extension by avoiding resolving to the wrong path

const process = await import('node:process');

relative = (file) => path.relative(process.cwd(), file);
relative = (file) => path.relative('.', file);
Copy link
Member Author

Choose a reason for hiding this comment

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

Apparently '.' is equivalent to process.cwd() so that helps simplify things here

Comment on lines +10 to +20
it('should be the exact version from package.json', async () => {
const { VERSION } = await import(join(import.meta.dirname, 'version.js'));
const pkg = JSON.parse(
readFileSync(fileURLToPath(new URL('../package.json', import.meta.url)), 'utf-8')
);
assert.equal(
VERSION,
pkg.version,
'VERSION export in src/version.js does not equal version in package.json'
);
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to put the test logic inside of it so Vitest would stop skipping it from the IDE extension


typescript: {
config: (config) => {
config.include.push('../unit-test');
Copy link
Member Author

Choose a reason for hiding this comment

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

Avoids a typescript error from displaying in the unit test file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Volatile paths in generated tsconfig.json in monorepo Sveltekit plugin breaks vitest workspaces

1 participant