question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`getExportedDeclarations` not working with yarn PnP

See original GitHub issue

Describe the bug

Version: 11.0.0

I’m in the process of migrating a project to Yarn v3, including the Plug’n’Play functionality.

My project is structured as a TypeScript monorepo. The documentation is generated using ts-morph. Since the switch, that is failing, because getExportedDeclarations() is returning an empty array for re-exported types from another package of the monorepo. Many other types have become any.

I suspect this is because ts-morph is not compatible with Yarn PnP out of the box, but I’m not sure how to make it work to be honest.

To Reproduce

This is somewhat tricky to reproduce, but you can use my repo to do so.

  1. clone https://github.com/zwave-js/node-zwave-js
  2. check out commit eb20fcb
  3. run yarn add --dev ts-morph (so yarn PnP doesn’t barf at you)
  4. create a file repro.ts with the following content in the root:
import { Project } from "ts-morph";

import { tsConfigFilePath } from "./packages/maintenance/src/tsAPITools";

const program = new Project({ tsConfigFilePath });

const file = program.getSourceFile("packages/zwave-js/src/index.ts");
const identifier = "ValueID";
const decl = file?.getExportedDeclarations().get(identifier);
debugger;
  1. run this in a debugging console: yarn node -r ts-node/register repro.ts

Expected behavior

decl should be an array with length 1, including the original symbol for ValueID.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
AlCalzonecommented, Jun 28, 2021

I was able to work around this for now by using yarn’s patch protocol to require typescript from within ts-morph with the PnP API enabled: 5085a05 (#2913)

1reaction
jwoo0122commented, Jul 4, 2022

It seems like the way ts-morph imports typescript cannot be effected by yarn PnP api. From this comment ts-morph decided to include typescript bundle as hard-copy. Because yarn enables PnP by patching typescript, including typescript like this way (not by using package json - dependencies) ts-morph cannot be compatible with yarn pnp.

So patching @ts-morph/common is one of the fast workarounds. Just fix import statement(this is similar to what @AlCalzone did, details can be changed). You can use yarn patch, patch-package, or anything else.

// in ts-morph-common.js
const ts = require('./typescript') // this one point out hard-copied typescript
// change to
const ts = require('typescript') //

I’m not sure if ts-morph should change this behavior, to support yarn pnp. Anyway this kind of bundle process (hard copy typescript dist file) is not regular way as I know, so we should think about it (although the comment said there was a hell when ts-morph serves typescript as dependency)…? @dsherret

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting | Yarn
If you get it, it very likely means that your application is not running with the PnP resolver, meaning that your require calls...
Read more >
ts-simple-ast - Bountysource
Hi, I'm working on some TS/JS projects, and this tool really helped me, ... Support automatically determining if a string literal should be...
Read more >
What is Yarn PNP and Should You Use It? - Atomic Spin
Yarn PNP (Plug'n'Play) can make installation faster and provide better strictness around transitive dependencies. Is it worth using?
Read more >
Getting started with Yarn 2: Ambiguous imports | by xsmith
After opening an issue over on Yarn, Maël mentioned a fix was to use Node.js' module. ... since it should only run when...
Read more >
Yarn not recognizing built in package? - Stack Overflow
I am now trying to migrate to Yarn 2 with PnP and in that, trying to fix anything yarn dlx @yarnpkg/doctor finds, as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found