webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.03k stars 174 forks source link

🔄 cspell json config with imports that aren't arrays now throws an error #825

Closed cylewaitforit closed 3 weeks ago

cylewaitforit commented 3 weeks ago

Prerequisites

Reproduction url

https://github.com/cylewaitforit/nx-knip

Reproduction access

Good version

5.33.3

Bad version

5.34.0

Description of the regression

The cspell config schema defines that import can be either a string path like in apps/app/.cspell.json or an array of string paths like in the root .cspell.json config file ( see cspell schema defintion https://github.com/streetsidesoftware/cspell/blob/5f974b2d4d61e2582aba5a1c87fcbc127f5f06bb/cspell.schema.json#L1884). Currently as of version 5.34.0 knip throws an error when using the single string path.

[*] Unresolved configuration (from CLI arguments)
{
  cacheLocation: '/Users/cyle/Repos/nx-knip/node_modules/.cache/knip',
  cwd: '/Users/cyle/Repos/nx-knip',
  excludedIssueTypes: [],
  fixTypes: [],
  gitignore: true,
  includedIssueTypes: [],
  isCache: false,
  isDebug: true,
  isDependenciesShorthand: false,
  isExportsShorthand: false,
  isFilesShorthand: false,
  isFix: false,
  isHideConfigHints: false,
  isIncludeEntryExports: false,
  isIncludeLibs: false,
  isIsolateWorkspaces: false,
  isProduction: false,
  isRemoveFiles: false,
  isShowProgress: false,
  isStrict: false,
  isWatch: false,
  tags: [ [], [] ],
  tsConfigFile: undefined,
  workspace: undefined
}
[*] Included issue types
{
  files: true,
  dependencies: true,
  devDependencies: true,
  optionalPeerDependencies: true,
  unlisted: true,
  binaries: true,
  unresolved: true,
  exports: true,
  nsExports: false,
  types: true,
  nsTypes: false,
  enumMembers: true,
  classMembers: false,
  duplicates: true
}
[*] Included workspaces
[ '@nx-knip/source' ]
[*] Included workspace configs
[
  {
    pkgName: '@nx-knip/source',
    name: '.',
    config: {
      entry: [
        '{index,cli,main}.{js,mjs,cjs,jsx,ts,tsx,mts,cts}!',
        'src/{index,cli,main}.{js,mjs,cjs,jsx,ts,tsx,mts,cts}!'
      ],
      project: [ '**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}!' ],
      paths: {},
      ignore: [],
      isIncludeEntryExports: false,
      cspell: {
        config: [ '.cspell.json', 'apps/app/.cspell.json' ],
        entry: null,
        project: null
      }
    },
    ancestors: []
  }
]
[*] Parsed gitignore files
{
  gitignoreFiles: [ '.git/info/exclude', '.gitignore', 'apps/app/.gitignore' ],
  ignores: Set(57) {
    '.git',
    '**/node_modules/**',
    '.yarn',
    '**/dist',
    '**/dist/**',
    '**/tmp',
    '**/tmp/**',
    'out-tsc',
    'out-tsc/**',
    '**/node_modules',
    '.idea',
    '.idea/**',
    '**/.project',
    '**/.project/**',
    '**/.classpath',
    '**/.classpath/**',
    '**/.c9',
    '**/.c9/**',
    '**/*.launch',
    '**/*.launch/**',
    '**/.settings',
    '**/.settings/**',
    '**/*.sublime-workspace',
    '**/*.sublime-workspace/**',
    '**/.vscode/*',
    '.sass-cache',
    '.sass-cache/**',
    'connect.lock',
    'connect.lock/**',
    'coverage',
    'coverage/**',
    'libpeerconnection.log',
    'libpeerconnection.log/**',
    '**/npm-debug.log',
    '**/npm-debug.log/**',
    '**/yarn-error.log',
    '**/yarn-error.log/**',
    '**/testem.log',
    '**/testem.log/**',
    'typings',
    'typings/**',
    '**/.DS_Store',
    '**/.DS_Store/**',
    '**/Thumbs.db',
    '**/Thumbs.db/**',
    '**/.nx/cache',
    '**/.nx/cache/**',
    '**/.nx/workspace-data',
    '**/.nx/workspace-data/**',
    'apps/app/**/.cache',
    'apps/app/**/.cache/**',
    'apps/app/**/build',
    'apps/app/**/build/**',
    'apps/app/**/public/build',
    'apps/app/**/public/build/**',
    'apps/app/**/.env',
    'apps/app/**/.env/**'
  },
  unignores: [
    '**/.vscode/settings.json',
    '**/.vscode/settings.json/**',
    '**/.vscode/tasks.json',
    '**/.vscode/tasks.json/**',
    '**/.vscode/launch.json',
    '**/.vscode/launch.json/**',
    '**/.vscode/extensions.json',
    '**/.vscode/extensions.json/**'
  ]
}
[.] Glob options
{
  patterns: [ '.travis.yml' ],
  cwd: '/Users/cyle/Repos/nx-knip',
  dir: '/Users/cyle/Repos/nx-knip',
  gitignore: true,
  absolute: true,
  dot: true,
  ignore: [
    '.git',
    '**/node_modules/**',
    '.yarn',
    '**/dist',
    '**/dist/**',
    '**/tmp',
    '**/tmp/**',
    'out-tsc',
    'out-tsc/**',
    '**/node_modules',
    '.idea',
    '.idea/**',
    '**/.project',
    '**/.project/**',
    '**/.classpath',
    '**/.classpath/**',
    '**/.c9',
    '**/.c9/**',
    '**/*.launch',
    '**/*.launch/**',
    '**/.settings',
    '**/.settings/**',
    '**/*.sublime-workspace',
    '**/*.sublime-workspace/**',
    '**/.vscode/*',
    '.sass-cache',
    '.sass-cache/**',
    'connect.lock',
    'connect.lock/**',
    'coverage',
    'coverage/**',
    'libpeerconnection.log',
    'libpeerconnection.log/**',
    '**/npm-debug.log',
    '**/npm-debug.log/**',
    '**/yarn-error.log',
    '**/yarn-error.log/**',
    '**/testem.log',
    '**/testem.log/**',
    'typings',
    'typings/**',
    '**/.DS_Store',
    '**/.DS_Store/**',
    '**/Thumbs.db',
    '**/Thumbs.db/**',
    '**/.nx/cache',
    '**/.nx/cache/**',
    '**/.nx/workspace-data',
    '**/.nx/workspace-data/**'
  ]
}
[.] Enabled plugins
[
  'CSpell',     'ESLint',
  'Nx',         'Playwright',
  'Prettier',   'Remix',
  'TypeScript', 'Vite',
  'Vitest'
]
[.] Definition paths (0)
[]
[.] Entry paths in package.json (0)
[]
[.] Glob options
{
  patterns: [ 'apps/app/.cspell.json', '.cspell.json' ],
  cwd: '/Users/cyle/Repos/nx-knip',
  dir: '/Users/cyle/Repos/nx-knip',
  gitignore: false,
  absolute: true,
  dot: true,
  ignore: [ '**/node_modules/**', '.yarn' ]
}
[.] CSpell config file paths (2)
[
  '/Users/cyle/Repos/nx-knip/.cspell.json',
  '/Users/cyle/Repos/nx-knip/apps/app/.cspell.json'
]
file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/plugins/cspell/index.js:13
    return (config.import ?? []).map(toDeferResolve);
                                 ^

TypeError: (config.import ?? []).map is not a function
    at Object.resolveConfig (file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/plugins/cspell/index.js:13:34)
    at runPlugin (file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/WorkspaceWorker.js:254:77)
    at async WorkspaceWorker.findDependenciesByPlugins (file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/WorkspaceWorker.js:280:13)
    at async main (file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/index.js:118:41)
    at async run (file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/cli.js:26:83)
    at async file:///Users/cyle/Repos/nx-knip/node_modules/.pnpm/knip@5.34.3_@types+node@22.8.1_typescript@5.5.3/node_modules/knip/dist/cli.js:103:1

Node.js v18.18.2

The work around at the moment is to update .cspell.json files to turn all the strings into arrays containing a single path.

webpro commented 3 weeks ago

:rocket: This issue has been resolved in v5.34.4. See Release 5.34.4 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

webpro commented 3 weeks ago

Btw I don't think this was actually a regression, right? Doesn't matter, needed a fix either way.