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

"Can't find stylesheet to import" when using --threads=false #1230

Closed
6 tasks done
oneillsp96 opened this issue May 3, 2022 · 11 comments · Fixed by vitejs/vite#11079
Closed
6 tasks done

"Can't find stylesheet to import" when using --threads=false #1230

oneillsp96 opened this issue May 3, 2022 · 11 comments · Fixed by vitejs/vite#11079
Labels
help wanted Extra attention is needed

Comments

@oneillsp96
Copy link

Describe the bug

"vitest run" works fine, tests pass
"vitest run --threads=false" errors out with "can't find stylesheet to import"

in my repro, styles.scss is imported into App.tsx, and it in turn imports a stylesheet called "colors.scss"

I was only using threads=false to try to get around a Node issue in my CI build, but it doesn't work.

Reproduction

https://github.com/oneillsp96/vitest-error-2

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 16.54 GB / 31.83 GB
  Binaries:
    Node: 16.14.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.5.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 100.0.4896.127
    Edge: Spartan (44.19041.1266.0), Chromium (101.0.1210.32)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.1
    vite: ^2.9.5 => 2.9.5
    vitest: ^0.9.3 => 0.9.3

Used Package Manager

yarn

Validations

@Crevitus
Copy link

I can confirm I am also encountering this issue on the latest version of vitest with threading turned off:

Error: Can't find stylesheet to import.
  ╷
2 │ @import "bootstrap/scss/bootstrap";
...

This is imported into my index.scss file. The main Vite build works.

Versions:

npmPackages:
   @vitejs/plugin-react: 1.3.2
   vite: 2.9.8 
   vitest: 0.12.4

@nathanchicken
Copy link

nathanchicken commented May 16, 2022

I'm getting this too, but doesn't matter if I'm using threads=false or not.

vite build runs fine though.

I have in my main config this

css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "sass:math";
            @import "@/styles/_variables.scss";
            @import "@/styles/_mixins.scss";
          `,
        },
      },

And npx vitest run gives me this:

Error: Can't find stylesheet to import.
 @import "@/styles/_variables.scss";
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^

I"ve tried a few combinations of filenames with and without aliases but to no avail.

If I change all imports to their absolute paths on my filesystem, eg:

@import '/Users/username/path-to-repo/src/styles/_variables.scss';

Then it starts working, so it's not resolving the aliases properly for SCSS as far as I can see, while in Vite itself it's all working fine.

@ChenYCL
Copy link

ChenYCL commented May 19, 2022

Hello,get same issue. lerna project , i need some scss file import from theme pkg.
image
Have any solution solve this? thx

@tansin
Copy link

tansin commented May 19, 2022

I have the same issue. I have found that the actual error occurs even though the resolution may have worked properly.

  1. The style is imported in the vue component like this:
<style lang="scss" scoped>
@import "../../../variables";
// ...
  1. When I start vitest I've got this error, as we all:
Can't find stylesheet to import.
  ╷
2 │ @import "../../../variables";
  │         ^^^^^^^^^^^^^^^^^^^^
  ╵
  1. Then I've deleted the style file and got the same error. (restored the file afterwards).
  2. Then I've added following resolve config to the vitest.config.ts. It just replaces the relative path string found in import directive by the actual absolute path. (I've tested also various regex patterns and file names getting same result, but this config here is the simplest one):
export default defineConfig({
  // ...
  resolve: {
    alias: [
      {
        find: "../../../variables",
        replacement: "/Users/user/git/project/_variables.scss",
      },
    ],
  },
})
  1. Run vitest and still got the same error. Wait, what? I've provided the correct absolute path as replacement. Hmm.
  2. But then I've deleted the style file again and got a new error:
Error: ENOENT: no such file or directory, open '/Users/user/git/project/_variables.scss'

It means, that even after resolving to absolute path manually the resolved path seems to not be consumed properly.

@nathanchicken
Copy link

The only real place I get this being an issue is in the preprocessorOptions part.

So to circumvent the issue for now I'm doing this to basically ensure that it's an absolute path. I'm basically duplicating the alias and it seems to be working. Within the SCSS files in the repo that a test they are using relative paths.

{
resolve: {
  {
      find: /^@\//,
      replacement: `${path.resolve(__dirname, 'src')}/`,
    },
},
// ...
css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "sass:math";
            @import "${path.resolve(__dirname, 'src')}/styles/_variables.scss";
            @import "${path.resolve(__dirname, 'src')}/styles/_mixins.scss";
            
          `,
        },
      },
    },
 }

@ChenYCL
Copy link

ChenYCL commented May 20, 2022

// vite.config.ts  it works

resolve: {
  alias: [
    {
      find: /^~@lui/,
      replacement: `${path.resolve(__dirname, 'packages')}/`,
    },
  ],
},

@yichengsong318
Copy link

I am getting the same issue.
Vue3 + quasar

// vite.config.js
quasar({
sassVariables: 'src/styles/quasar.variables.scss'
})

@nicolas-camacho
Copy link

Since it works with absolute routes, I ended up creating an alias for my variables file

Project info

Framework: Next JS

vitest.config.ts

export default defineConfig({
  plugins: [react()],
  test: {
    environment: 'jsdom',
  },
  resolve: {
      alias: [
          {
              find: 'variables',
              replacement: `${path.resolve(__dirname)}/styles/variables.scss`
          }
      ]
  }
})

{file}.scss

@import 'variables';

It is working for me right now but it is not an optimal solution

@Lyokolux
Copy link

Lyokolux commented Jun 27, 2022

Ok it seems that relative import are broken.
I have the same issue as #1230 (comment)

It happens with or without --threads=false.

If the main scss files import other files, then the paths have to be absolute, else it will return the error Can't find stylesheet to import..

My workaround (for one file):

import { defineNuxtConfig } from 'nuxt'
import { resolve } from 'path'

const r = (p: string) => resolve(__dirname, p)

export default defineNuxtConfig({
  vite: {
    css: {
      preprocessorOptions: {
        scss: { additionalData: `@use "${r('assets/scss/global.scss')}";` },
      },
    }
  }
})

If there is multiple files, my workaround becomes:

css: {
      preprocessorOptions: {
        scss: { additionalData: `@use "${r('assets/scss/global.scss')}";@use "${r('assets/scss/reset.scss')}";@use "${r('assets/scss/typography.scss')}";@use "${r('assets/scss/fonts.scss')}";@use "${r('assets/scss/spacing.scss')}";@use "${r('assets/scss/dripicons.scss')}";@use "${r('assets/scss/colors.scss')}"; @use "${r('assets/scss/utils/layout.scss')}"; @use "${r('assets/scss/utils/overflow.scss')}"; ` },
      },
    }

Basically chaining the @use directive for each file + commenting the imports in my global.scss that imports all these files OR not importing the global.scss file.

@alexmccabe
Copy link

alexmccabe commented Sep 28, 2022

None of the above suggestions seem to be working for me

const resolve = (filePath: string) => {
  return path.resolve(__dirname, filePath);
};
css: {
  preprocessorOptions: {
    scss: {
      additionalData: [
        // Shared global scss utilities.
        `@import "${resolve('src/scss/00-base/index.scss')}";`,
        '',
      ].join('\n'),
    },
  },
},
test: {
  threads: false,
}

CleanShot 2022-09-28 at 16 13 56@2x

@alexmccabe
Copy link

The workaround I got to behave was:

test: {
 minThreads: 0,
 maxThreads: 1,
}

This seems silly since this is effectively doing the same thing? 🤔

@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants