pants icon indicating copy to clipboard operation
pants copied to clipboard

Add package support to typescript/javascript dependency inference

Open jasondamour opened this issue 1 year ago • 3 comments

This adds better dependency inference to typescript/javascript.

  1. Adds support for imports relative to tsconfig root a. i.e. utils/stringUtils -> frontend/src/utils/stringUtils.ts
  2. Adds support for namespaced package imports a. @apollo/client -> :package#__apollo/client
  3. Adds support for importing .tsx from .ts files a. This is a weird one, I had to add this to support my usecase but I'm not sure the "right" way to handle js/jsx/ts/tsx cross-imports.

I'm not how much of the javascript "spec" I covered

Sample file

# example.ts
import 'axios'
import '@apollo/client'
import 'utils/stringUtils'
import 'utils/tableUtils'
import '../utils/urlUtils'

Produces

PANTS_SOURCE=../../OSS/pants pants --no-pantsd dependecies example.ts
frontend/src/utils/stringUtils.ts:../../typescript
frontend/src/utils/tableUtils.tsx:../../tsx
frontend/src/utils/urlUtils.ts:../../typescript
frontend:package#__apollo/client
frontend:package#axios

jasondamour avatar Oct 18 '24 23:10 jasondamour

You need to provide the tsconfig or full repro to be able to validate your example.

"Adds support for imports relative to tsconfig root", this is not a default of the ts resolution spec, but might be for your tooling. If it is, I'm going to encourage you set your tsconfig to match what the tooling is implicitly doing, instead of merging "1a" of this change.

You generally need to supply "paths" entries in the tsconfig that maps to the "any" match (*) to get behaviour like that, from what I understand. See https://www.typescriptlang.org/tsconfig/#paths and https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths

If tests prove "2a" I think that would be a good bug fix.

3a should adhere to https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution.

I just want to reiterate that the parser is a javascript parser, not a typescript parser. It is not up to the task of parsing any typescript specifics, so please tread lightly.

tobni avatar Nov 03 '24 21:11 tobni

@jasondamour thanks for this contribution! Do you think you will be able to revise with the review comments?

cburroughs avatar Jan 09 '25 20:01 cburroughs

Just a headsup that I think this is superseded by https://github.com/pantsbuild/pants/pull/22364 (and prior changes).

tobni avatar May 25 '25 16:05 tobni