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.

[Bug]: Unable to install packages from CodeSandbox

See original GitHub issue

Self-service

  • I’d be willing to implement a fix

Describe the bug

Package URLs from CodeSandbox doesn’t include tgz so Yarn is unable to install them

Originally reported by @phryneas

To reproduce

const installPromise = packageJsonAndInstall({
	dependencies: {
		[`@reduxjs/toolkit`]: `https://pkg.csb.dev/reduxjs/redux-toolkit/commit/7491320d/@reduxjs/toolkit`,
	},
});

await expect(installPromise).resolves.toBeTruthy();

Environment

  System:
    OS: Windows 10 10.0.19042
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 16.7.0 - ~\AppData\Local\Temp\xfs-d3717859\node.CMD
    Yarn: 3.0.1-git.20210819.hash-32881f98b - ~\AppData\Local\Temp\xfs-d3717859\yarn.CMD
    npm: 7.20.1 - C:\nodejs\npm.CMD

Additional context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ylemkimoncommented, Aug 26, 2021

A workaround would be to use a url hash: yarn add @reduxjs/toolkit@https://pkg.csb.dev/reduxjs/redux-toolkit/commit/7491320d/@reduxjs/toolkit#.tgz.

0reactions
Andaristcommented, Dec 16, 2021

Until CSB fixes how they are generating package.json manifests, this plugin can be used as a workaround:

module.exports = {
  name: 'fix-csb-deps',
  factory: (require) => {
    const crypto = require('crypto')

    // https://github.com/yarnpkg/berry/blob/5411f76bcd89d1d6f430f4bd0e16146ce9fdd370/packages/yarnpkg-core/sources/hashUtils.ts#L5-L26
    function makeHash(...args) {
      const hash = crypto.createHash(`sha512`)

      let acc = ``
      for (const arg of args) {
        if (typeof arg === `string`) {
          acc += arg
        } else if (arg) {
          if (acc) {
            hash.update(acc)
            acc = ``
          }

          hash.update(arg)
        }
      }

      if (acc) hash.update(acc)

      return hash.digest(`hex`)
    }

    return {
      hooks: {
        reduceDependency(dependency, project, locator, initialDependency, { resolver, resolveOptions }) {
          if (dependency.range.startsWith('https://pkg.csb.dev/') && !dependency.range.endsWith('/_pkg.tgz')) {
            const newRange = `${dependency.range}/_pkg.tgz`
            const fixedDescriptor = {
              identHash: dependency.identHash,
              scope: dependency.scope,
              name: dependency.name,
              descriptorHash: makeHash(dependency.identHash, newRange),
              range: newRange,
            }
            return fixedDescriptor
          }
          return dependency
        },
      },
    }
  },
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How we make npm packages work in the browser
There is a great advantage to this approach: we will be able to install and cache dependencies independently. We can just merge the...
Read more >
FAQs - CodeSandbox
There are a few possible reasons a repo might throw that error on import. The most common are either a lack of a...
Read more >
step1-install-dependencies - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
Installing and using an NPM module - CodeSandbox
Installing and using an NPM module ... package.json ... Activating extension 'vscode.typescript-language-features' failed: Could not find bundled ...
Read more >
install - CodeSandbox
edeityedeity. Templateuse-plugin; Environmentcreate-react-app. Files. public. src. App.js. index.js. styles.css. package.json. Dependencies.
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