Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__dirname inaccurate in TS gatby-configs #35488

Closed
2 tasks done
mfanuzzi opened this issue Apr 25, 2022 · 2 comments
Closed
2 tasks done

__dirname inaccurate in TS gatby-configs #35488

mfanuzzi opened this issue Apr 25, 2022 · 2 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@mfanuzzi
Copy link

Preliminary Checks

Description

In porting some older Gatsby 3 projects to 4, I also converted the gatsby-config.js to .ts as we use Typescript and typesafe configs are 💯 🏆 🔥

Our gatsby-configs tend to use __dirname (eg for gatsby-plugin-root-import, gatsby-source-filesystem, etc.) This does not work as intended in TS gatsby-configs. Presumably, because of transpiling, __dirname will instead reference [project]/.cache/compiled

Reproduction Link

https://github.com/gatsbyjs/gatsby/tree/master/starters/default

Steps to Reproduce

I created two basic starters gatsby new, one TS, one JS, and added to each's gatsby-config:

console.log(`__dirname is ${__dirname}`);

Output of gatsby build for JS version (correct):

__dirname is /home/mfan/projects/experimental/dirname-test/dirname-test-js

Output of gatsby build for TS version (incorrect):

__dirname is /home/mfan/projects/experimental/dirname-test/dirname-test-ts/.cache/compiled

Expected Result

__dirname should be the project source directory

Actual Result

__dirname will instead reference [project]/.cache/compiled for TS gatsby-configs

Environment

System:
    OS: Linux 4.19 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  npmPackages:
    gatsby: ^4.12.1 => 4.12.1
  npmGlobalPackages:
    gatsby-cli: 4.12.1

Config Flags

No response

@mfanuzzi mfanuzzi added the type: bug An issue or pull request relating to a bug in Gatsby label Apr 25, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 25, 2022
@mfanuzzi
Copy link
Author

mfanuzzi commented Apr 25, 2022

Here is my (hopefully temporary) solution, at the top of gatsby-config.ts after my imports:

// __dirname gets changed when this file is precompiled; change it back
if (__dirname.endsWith('/.cache/compiled')) {
  __dirname = __dirname.replace('/.cache/compiled', '');
}

Better would be if it did this automatically. Or if there was some other, "safe" variable that we could use to accomplish the same thing (eg __projectroot)

@mfanuzzi mfanuzzi changed the title __dirname innacurate in TS gatby-configs __dirname inaccurate in TS gatby-configs Apr 25, 2022
@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 26, 2022
@LekoArts
Copy link
Contributor

Hi!

This is a known limitation: https://www.gatsbyjs.com/docs/how-to/custom-configuration/typescript/#__dirname
I fixed it with parcel-bundler/parcel#7727 which got released in Parcel 2.5.0 and the PR #35446 will update to that version.

@LekoArts LekoArts closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants