Skip to content

Transform readFile/writeFile methods are not called #3022

@ye-yu

Description

@ye-yu

I have:

I have a sample code fix on readFile in my fork that I would like to contribute to this repository as a fix to this issue. I would like to obtain suggestions and discussions on how to fix this issue before submitting a PR.

Bug description

Custom compiler transformers extending Transform do not reliably receive readFile / writeFile calls during compilation. This prevents transformer implementations from intercepting file I/O and breaks transform-based file resolution.

Actual behavior

  • Transform.prototype.readFile / writeFile are not properly wired for derived transformer classes.
    super.readFile(filename, baseDir) can fail or be skipped.
  • When multiple transformers are configured, the first transformer that returns a result should short-circuit, but current handling does not consistently enforce that.

Expected behavior

  • A transformer subclass should be able to override readFile and writeFile and have those methods called during compilation.
  • Calling super.readFile(...) inside an override should delegate to the next transform or the default loader.
    If multiple transformers are configured, the first non-null result should be used and remaining transformers should not override it.

My use case: another source to source on top of AS transpiler

I would like to develop a feature that will transform unsupported syntaxes to an intermediary source to work around some limitation. I'd figure that a transformer that was promised to me in this documentation Compiler - Transforms - readFile would work. However, it did not return the expected result :(

Steps to reproduce

  1. In tests/transform/index.js, add a tracking variable readFileCalled and override readFile method to update this variable

    var constructorCalled = false;
    var afterParseCalled = false;
    var afterInitializeCalled = false;
    var afterCompileCalled = false;
    + var readFileCalled = false;
    
    console.log("Transform loaded");
    
    export default class MyTransform extends Transform {
      // ...
    
      readFile(filename, baseDir) {
    +     readFileCalled = true;
        return super.readFile(filename, baseDir);
      }
    
      // ...
  2. Assert readFile is called on afterParse

  3. Run npm run test:transformer

Expected result: readFileCalled is true
Actual result: readFileCalled is false

AssemblyScript version

v0.28.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions