ojkelly/yarn.build

[BUG] bundle.tgz becomes corrupted while using docker

Closed this issue · 8 comments

Describe the bug
Bundling or adding bundle file to Dockerfile

To Reproduce


# First stage
FROM node:14.15.1-alpine as bundler
WORKDIR usr/src/app

ADD . .

RUN yarn install
RUN yarn workspace @services/service bundle

WORKDIR services/service 
RUN yarn bundle
RUN apk update
RUN apk add tar
RUN ls -la ./
RUN tar xvf bundle.tgz

Step 9/14 : RUN tar xvf bundle.tgz
---> Running in 93349fea266e
gzip: invalid magic
tar: Child returned status 1
tar: Error is not recoverable: exiting now
The command '/bin/sh -c tar xvf bundle.tgz' returned a non-zero code: 2

Expected behavior
Extract files

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: macOS
  • yarn 2.4.0
  • yarn.build latest

Additional context
Able to extract it in terminal. Either mounted or bundled, bundle.tgz is corrupted.

I wonder if this could be the classic https://unix.stackexchange.com/questions/302192/how-to-solve-tar-invalid-magic-error-on-linux-alpine#302231

Can you give the steps in the link a go, and see if the issue still persists?

You’re use-case is definitely one that I want to support, so I’m keen to work out what the issue is.

I tried everything in this article.
I still getting error with tar or gzip updated in alpine image.
I also tested not using Docker RUN but shell script and its the same.

using gzip I am getting:

OK: 8 MiB in 19 packages
gzip: bundle.tgz has more than one entry -- unchanged
The command '/bin/sh -c ./bundle.sh' returned a non-zero code: 1

https://superuser.com/questions/1237854/gzip-stdin-has-more-than-one-entry-rest-ignored-and-gzip-tmp-gz-has-more-t
This article says it could be due to archive being archived different format than the extension. Or some checksum issues because if I copy the archive to the alpine and try to extract it throws same errors.

I believe this is caused by the use of @yarnpkg/libzip which is producing a literal zip archive file and not a tgz (tar+gzip) as the output has been named.

e.g. of yarn's usage to create an archive.zip.
https://github.com/yarnpkg/berry/blob/2b477b49b93985bbf7ffd4149290dace6a96d231/packages/yarnpkg-core/sources/tgzUtils.ts#L15-L32

As a workaround; I was able to use unzip bundle.tgz on the file successfully from an alpine container.

Further reviewing the yarn pack command. It might be possible to make use of the @yarnpkg/plugin-pack module packUtils to create a properly formatted tgz file?

Entrypoint:
https://github.com/yarnpkg/berry/blob/257ba6e6ec6b32fc4f1f78ff3c35271e3a8e876d/packages/plugin-pack/sources/commands/pack.ts#L83-L84

Module:
https://github.com/yarnpkg/berry/blob/257ba6e6ec6b32fc4f1f78ff3c35271e3a8e876d/packages/plugin-pack/sources/packUtils.ts#L66-L132

Hmm, would it make sense in the short term just to have it output archive.zip, and look at making a proper .tgz at a later date?

As an example, below works correctly, bundle.tgz not working as of release 0.9.52.

FROM node:14.15.1-alpine as bundler
WORKDIR usr/src/app

ADD . .

RUN yarn install

WORKDIR services/auth
RUN yarn bundle -a bundle.zip
RUN apk update && apk add zip
RUN unzip bundle.zip

I've just released v0.9.53, which has a fix to output bundle.zip instead of .tgz.

Can you confirm if latest is now working correctly?

Working.