Skip to content

Comments

feat(vite): allow setting custom cwd path#15188

Open
polvallverdu wants to merge 1 commit intosveltejs:mainfrom
polvallverdu:feat/sveltekit-cwd-option
Open

feat(vite): allow setting custom cwd path#15188
polvallverdu wants to merge 1 commit intosveltejs:mainfrom
polvallverdu:feat/sveltekit-cwd-option

Conversation

@polvallverdu
Copy link

@polvallverdu polvallverdu commented Jan 20, 2026

closes #13748

I have a monorepo with a package being a sveltekit app. I do unit testing, but it was completely failing when running vitest from the monorepo due to aliases not working, and mainly, the vite sveltekit plugin not finding the svelte.config.js file.

I fixed it with a patch allowing to override the cwd (solution proposed on the linked issue), and setting the cwd like this:

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig({
  plugins: [
    tailwindcss(),
      sveltekit({
      cwd: __dirname,
    }),
  ],
  test: {
    name: "redacted",
    projects: [
      {
        extends: "./vite.config.ts",
        test: {
          name: "client",
          browser: {
            enabled: true,
            provider: playwright(),
            instances: [{ browser: "chromium", headless: true }],
          },
          include: ["tests/**/*.svelte.test.ts"],
        },
      },
      {
        extends: "./vite.config.ts",
        test: {
          name: "server",
          environment: "node",
          include: ["tests/**/*.test.ts"],
          exclude: ["tests/**/*.svelte.test.ts"],
        },
      },
    ],
  },
});

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 Jan 20, 2026

🦋 Changeset detected

Latest commit: 101ac8e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

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

@svelte-docs-bot
Copy link

@benmccann
Copy link
Member

Please see #12420 (comment) and #12410 (comment). I'm not sure this is necessary

@elliott-with-the-longest-name-on-github
Copy link
Contributor

I also think we should lean on monorepo tools for this if possible -- if you have a use case that doesn't work with pnpm --dir or some similar option, please share a reproduction!

@polvallverdu
Copy link
Author

polvallverdu commented Jan 21, 2026

Please see #12420 (comment) and #12410 (comment). I'm not sure this is necessary

Main issue here is that, even though vitest correctly sets the root directory, the sveltekit plugin overrides it to process.cwd(), which is incorrect, and breaks monorepo compatibility. Maybe a better solution would be the sveltekit plugin to rely on the root from vitest (or vite) rather than process.cwd(), but that might be a bigger breaking change.

I also think we should lean on monorepo tools for this if possible -- if you have a use case that doesn't work with pnpm --dir or some similar option, please share a reproduction!

In CI a single vitest command tests all the packages of my monorepo, I don't think needing a different command for testing the sveltekit app package is good DX. I'll share a repro hopefully today.

@benmccann
Copy link
Member

Maybe a better solution would be the sveltekit plugin to rely on the root from vitest (or vite) rather than process.cwd()

SvelteKit has to tell Vite where the root is. There's no way to do it in the other direction

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.

allow passing configFile option to kit() in vite config

3 participants