smeijer / unimported

Find and fix dangling files and unused dependencies in your JavaScript projects.
MIT License
1.97k stars 71 forks source link

`-u` option repetitively adding the same entry into `ignoreUnresolved` #190

Closed foucdeg closed 12 months ago

foucdeg commented 1 year ago

In the file app/scripts/i18n.tsx I have the following code:

// In staging and prod, the SSR code loaded from the dist directory (output dir of Babel, see build-ssr)
// so the path to the locale files is not the same as in dev.
try {
  esTranslations = require("../../../app/assets/locales/es/translation.json")
} catch (e) {
  esTranslations = require("../assets/locales/es/translation.json")
}

unimported correctly flags the first import as unresolved. (In this environment the target file is located at the 2nd path)

Running npx unimported -u adds the following entry to ignoreUnresolved:

  "ignoreUnresolved": [
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
  ]

but it adds it repetitively; each time I run the command, the entry is added again.

After running the command three times, ignoreUnresolved looks like this:

  "ignoreUnresolved": [
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]]
  ]

No matter how many times this entry is present in ignoreUnresolved , npx unimported returns a non-zero status and complains about this unresolved import.

I've tried to ignore this error by adding into ignorePatterns :

    "../../../app/assets/locales/es/**",
    "app/assets/locales/es/**",
    "app/scripts/i18n.tsx",

but neither of these fixed the issue.

Current workaround is using npx unimported --show-unused-files --show-unused-deps.

foucdeg commented 1 year ago

This seems to be reproductible for any import syntax; after adding another entrypoint that includes

import Layout from "@theme/Layout" // <- unresolved

this entry also gets added to the config file when using -u despite being already present. After three runs of npx unimported -u I get:

  "ignoreUnresolved": [
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
    ["../../../app/assets/locales/es/translation.json", ["app/scripts/i18n.tsx"]],
    ["@theme/Layout", ["docusaurus/src/pages/index.tsx"]],
    ["@theme/Layout", ["docusaurus/src/pages/index.tsx"]],
    ["@theme/Layout", ["docusaurus/src/pages/index.tsx"]]
  ],

and a non-zero exit status every time for npx unimported.

foucdeg commented 1 year ago

It seems that the issue is when building the index of unresolvedImports:

https://github.com/smeijer/unimported/blob/main/src/process.ts#L32

Since each entry is not a simple string, this index doesn't have the shape it should.

foucdeg commented 1 year ago

Opened #191 with a fix.

foucdeg commented 12 months ago

Hi @smeijer I see you've responded to more recent issues recently, have you read this one and the PR?

smeijer commented 12 months ago

:tada: This issue has been resolved in version 1.31.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

smeijer commented 12 months ago

Sorry about the delay @foucdeg , just slipped my mind. Also, appreciate the extensive explanation. Definitely simplified the review :)