Code Monkey home page Code Monkey logo

Comments (9)

webpro avatar webpro commented on July 2, 2024

Any chance you could create a reproduction of the issue or add a failing case to one of the test fixtures?

And what's the first version of Knip this fails in for you?

from knip.

nihalgonsalves avatar nihalgonsalves commented on July 2, 2024

It stopped working after 5.16.0. Here's a minimal reproduction:

  • npm init -y

  • entry.ts

    require("./child");
  • child.ts

    function foo() {}
    
    foo();
  • knip.jsonc

    {
      "$schema": "https://unpkg.com/knip@5/schema.json",
      "entry": ["./entry.ts"]
    }
  • Tests:

    $ npx [email protected]
    ✂️  Excellent, Knip found no issues.
    
    $ npx [email protected]
    Unused files (1)
    child.ts
    
    $ npx [email protected]
    Unused files (1)
    child.ts

I couldn't reproduce it with .js files, so maybe that's a hint.

from knip.

webpro avatar webpro commented on July 2, 2024

I couldn't reproduce it with .js files, so maybe that's a hint.

It is. Isn't it odd to require() ts files without an extension? It's not that common, I wonder what tooling/bundler we're using here? And/or what TS config settings?

from knip.

nihalgonsalves avatar nihalgonsalves commented on July 2, 2024

Hmm I don't think it's unusual. Using tsc and running the output with node was the default basic Node + TypeScript setup (on the backend) until pretty recently.

I suppose using require with that setup is perhaps uncommon, as tsc does compile import statements for CJS. The case I ran into was using an entrypoint with require('reflect-metadata'); require('./app'); (I was able to change the knip entry as a workaround).

from knip.

webpro avatar webpro commented on July 2, 2024

I really need an actual reproduction of the issue. The basics work, I just added 0c56610, perhaps its specific TS config settings?

from knip.

nihalgonsalves avatar nihalgonsalves commented on July 2, 2024

Does my previous reproduction not work? That was without any tsconfig in the directory at all – just those three files and the default package.json from npm init -y

from knip.

webpro avatar webpro commented on July 2, 2024

Copy-pasting something here is not a reproduction. No tsconfig.json is most likely the problem. In isolation, why would you expect that to work? Without context I don't understand the situation/use case here.

from knip.

nihalgonsalves avatar nihalgonsalves commented on July 2, 2024

I was trying to make it as minimal as possible .. wouldn't tsconfig.json just imply some default settings anyway?

Here's my initial reproduction with a bare tsconfig.json added; it doesn't seem to make a difference to the knip output: https://codesandbox.io/p/devbox/knip-issue-681-xkv92z

I'm not sure I understand why you wouldn't expect that to work? It's a valid program that can be run via tsc and then node on the output. I added those scripts to the sandbox too.

from knip.

webpro avatar webpro commented on July 2, 2024

Thanks for the reproduction.

The underlying issue is that require() uses node dist/entry.js from the package.json#main as a starting point. And dist/*.js is also part of the project files by default, you probably don't want that.

So my recommendation would be to split up src and dist directories, eg:

{
    "$schema": "https://unpkg.com/knip@5/schema.json",
    "entry": ["src/entry.ts"],
    "project": ["src/*.ts"],
}

Not sure if this also is the "fix" for OP, but based on this repro I think the refactoring I did more reveals an issue in cases like this than that it introduces a regression. That split is also better for performance, as Knip does/should not wander off into compiled build artifacts.

from knip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.