Skip to content

Send local fetch error to verbose reporter before falling back to external fetch#6169

Open
itsananderson wants to merge 2 commits intoyarnpkg:masterfrom
itsananderson:report-exceptions-fetching-from-local
Open

Send local fetch error to verbose reporter before falling back to external fetch#6169
itsananderson wants to merge 2 commits intoyarnpkg:masterfrom
itsananderson:report-exceptions-fetching-from-local

Conversation

@itsananderson
Copy link

@itsananderson itsananderson commented Jul 28, 2018

Summary

Finally got a chance to put together a PR for the offline install enhancement (#5695) I submitted a few months ago.

Today, if Yarn is unable to install a package locally (local cache or offline mirror), it falls back to installing from an external source. However, if you've configured Yarn with --offline, the external fetch immediately fails with an exception.

What's tricky is the exception from fetchFromLocal is swallowed, so the reason for the failure is lost. This makes it much harder to debug corrupted archives etc.

This change updates the fallback to log the fetchFromLocal exception to the verbose reporter, so that it can be viewed if the --verbose CLI flag was provided. I opted to print the error in 2 lines because formatting the actual error message directly into the errorFetchingFromLocal message caused the final output to have \ escapes for all the quotes, which reduced readability.

Test plan

I first tested this with a missing package archive, which resulted in this output:

verbose 3.761 Error fetching package from local disk cache: "\"https://registry.yarnpkg.com/react/-/react-16.4.1.tgz\": Tarball is not in network and can not be located in cache ([\"/Users/itsananderson/test/yarn_packages/react-16.4.1.tgz\",\"/Users/itsananderson/Library/Caches/Yarn/v1/npm-react-16.4.1-de51ba5764b5dbcd1f9079037b862bd26b82fe32/.yarn-tarball.tgz\"])"
verbose 3.79 Error: Can't make a request in offline mode ("https://registry.yarnpkg.com/react/-/react-16.4.1.tgz")
    at RequestManager.request (/Users/itsananderson/sandbox/yarn/lib/util/request-manager.js:194:29)
    at NpmRegistry.request (/Users/itsananderson/sandbox/yarn/lib/registries/npm-registry.js:227:32)
    at /Users/itsananderson/sandbox/yarn/lib/fetchers/tarball-fetcher.js:208:33
    at Generator.next (<anonymous>)
    at step (/Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at new Promise (<anonymous>)
    at new F (/Users/itsananderson/sandbox/yarn/node_modules/core-js/library/modules/_export.js:35:28)
    at /Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
    at TarballFetcher.fetchFromExternal (/Users/itsananderson/sandbox/yarn/lib/fetchers/tarball-fetcher.js:268:7)
error Can't make a request in offline mode ("https://registry.yarnpkg.com/react/-/react-16.4.1.tgz")

Then I tested with an intentionally corrupted archive, which produced this:

verbose 1.993 Error fetching package from local disk cache:
verbose 1.993 Extracting tar content of "/Users/itsananderson/client/virtual_env/yarn_packages/react-16.4.1.tgz" failed, the file appears to be corrupt: "Unexpected end of data"
verbose 2.02 Error: Can't make a request in offline mode ("https://registry.yarnpkg.com/react/-/react-16.4.1.tgz")
    at RequestManager.request (/Users/itsananderson/sandbox/yarn/lib/util/request-manager.js:194:29)
    at NpmRegistry.request (/Users/itsananderson/sandbox/yarn/lib/registries/npm-registry.js:227:32)
    at /Users/itsananderson/sandbox/yarn/lib/fetchers/tarball-fetcher.js:208:33
    at Generator.next (<anonymous>)
    at step (/Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at new Promise (<anonymous>)
    at new F (/Users/itsananderson/sandbox/yarn/node_modules/core-js/library/modules/_export.js:35:28)
    at /Users/itsananderson/sandbox/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
    at TarballFetcher.fetchFromExternal (/Users/itsananderson/sandbox/yarn/lib/fetchers/tarball-fetcher.js:268:7)
error Can't make a request in offline mode ("https://registry.yarnpkg.com/react/-/react-16.4.1.tgz")

Since this diff is only adding verbose logging, it didn't make a ton of sense to add new tests, but if more tests are desired here, let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant