Skip to content

Cannot read properties of undefined (reading 'filename') #258

Closed

Description

@remarkablemark

Bug Report

Summary

Cannot read properties of undefined (reading 'filename') started occurring after the fflate dependency released version 0.8.3.

Steps to Reproduce

Clone any npm repository and run:

npx @arethetypeswrong/cli --pack

Instead of seeing success, process completes with exit code 3:

Error: Cannot read properties of undefined (reading 'filename')

See example.

Cause

Because the dependency version uses a caret ^, the latest version gets installed:

Solution

The current workaround is to add an npm override in package.json to pin fflate to 0.8.2:

"overrides": {
  "fflate": "0.8.2"
}

I also created an issue in fflate: 101arrowz/fflate#258

Activity

  1. 101arrowz commented on May 16, 2026

    @101arrowz

    This is caused by a change in the internal implementation of Gunzip in fflate. The code here seems to assume that one chunk in produces one chunk out with Gunzip, but this is not a guarantee of the API and was only an implementation detail of earlier versions of fflate. v0.8.3 introduced changes that may produce multiple output chunks for each input chunk, which caused things to break here.

    This patch fixes the issue:

    diff --git a/node_modules/@arethetypeswrong/core/dist/createPackage.js b/node_modules/@arethetypeswrong/core/dist/createPackage.js
    index 1b8fb1c..68d1492 100644
    --- a/node_modules/@arethetypeswrong/core/dist/createPackage.js
    +++ b/node_modules/@arethetypeswrong/core/dist/createPackage.js
    @@ -11,7 +11,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
     };
     var _Package_files;
     import { untar } from "@andrewbranch/untar.js";
    -import { Gunzip } from "fflate";
    +import { Gunzip, FlateErrorCode } from "fflate";
     import { major, maxSatisfying, minor, valid, validRange } from "semver";
     import ts from "typescript";
     import { parsePackageSpec } from "./utils.js";
    @@ -224,8 +224,21 @@ export function createPackageFromTarballData(tarball) {
     function extractTarball(tarball) {
         var _a;
         // Use streaming API to work around https://github.com/101arrowz/fflate/issues/207
    -    let unzipped;
    -    new Gunzip((chunk) => (unzipped = chunk)).push(tarball, /*final*/ true);
    +    const chunks = [];
    +    try {
    +        new Gunzip((chunk) => chunks.push(chunk)).push(tarball, /*final*/ true);
    +    } catch (err) {
    +        // this happens for zero-padded tarballs; can safely ignore
    +        if (err.code != FlateErrorCode.InvalidHeader) {
    +            throw err;
    +        }
    +    }
    +    const unzipped = new Uint8Array(chunks.reduce((a, b) => a + b.length, 0));
    +    let offset = 0;
    +    for (const chunk of chunks) {
    +        unzipped.set(chunk, offset);
    +        offset += chunk.length;
    +    }
         const data = untar(unzipped);
         const prefix = data[0].filename.substring(0, data[0].filename.indexOf("/") + 1);
         const packageJsonText = (_a = data.find((f) => f.filename === `${prefix}package.json`)) === null || _a === void 0 ? void 0 : _a.fileData;

    You should be able to do something similar in the TypeScript source to fix the issue!

    You may also be able to switch the implementation to const unzipped = gunzipSync(tarball);. That will work for any package without zero-padding at the end of the npm tarball (which seems to be rare/a bug).

  2. PineBale commented on May 16, 2026

    @PineBale
  3. PerryFinn commented on May 17, 2026

    @PerryFinn

    I encountered the same problem

  4. PineBale commented on May 17, 2026

    @PineBale
    Contributor

    hi folks, I banged my head against walls and AIs and found nothing working for my project, as I cannot just install the cli in devDependencies, which will override one of my existing dependencies. Do you think pinpointing fflate to 0.8.2 in packages/core/package.json then releasing new minor update can mitigate this issue? I run so sick of the issue today 😅 😤

  5. jedwards1211 commented on May 18, 2026

    @jedwards1211

    @andrewbranch using ^ for zero versions is just asking for trouble. Per the semver spec, any public API may change at any time between 0.x.y versions.

    Also, I would rather see this use a more stable compression library that's >= 1.0.0 instead of an unstable library just because it hypes its size and speed.

  6. added a commit that references this issue on May 18, 2026
    6cd564e
  7. jedwards1211 commented on May 19, 2026

    @jedwards1211

    @andrewbranch it would be good to check if the array returned by untar is empty and print a better error message in that case, because a tarball that happens to be empty for some reason would also cause this "Cannot read properties of undefined" error message. It would probably also be good to print a warning if there are garbage entries after the zero block in the archive, or if the last record is shorter than the rest.

  8. earshinov commented on May 21, 2026

    @earshinov
    Contributor

    @101arrowz , thanks for the patch. Checked it locally -- seems to be working fine. Submitted PR #261

  9. 18 remaining items

  10. added a commit that references this issue on Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions