Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double import issue #58

Merged
merged 9 commits into from
Nov 27, 2024
Merged

fix: double import issue #58

merged 9 commits into from
Nov 27, 2024

Conversation

0xteddybear
Copy link
Contributor

No description provided.

});
it('should replace path as instructed by remapping with all import syntaxes', function () {
const fileContent = `
import '../../../interfaces/ITest.sol';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong here, but I don't think remappings work this way. If your remapping key is interfaces, ../../../interfaces should not be affected, only when it starts with interfaces/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 will check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forge does a weird thing where it complains in the logs it cant find the files, as if it only used a remapping when the key is at the start of the imported path as you describe, but then replaces the key anywhere and manages to build the project. as a PoC, I:

  • ran forge init
  • in that repo, added wherecodeis/=src/
  • then edited the Counter import to import {Counter} from "../wherecodeis/Counter.sol";

and got this log:

[N] ~/p/remappingexample (master){2}
> forge test
[⠊] Compiling...
[⠒] Unable to resolve imports:
      "../wherecodeis/Counter.sol" in "/home/user/playground/remappingexample/test/Counter.t.sol"
with remappings:
      wherecodeis/=/home/user/playground/remappingexample/src/
      forge-std/=/home/user/playground/remappingexample/lib/forge-std/src/
[⠰] Compiling 25 files with Solc 0.8.26
[⠔] Solc 0.8.26 finished in 1.20s
Compiler run successful!

Ran 2 tests for test/Counter.t.sol:CounterTest
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 31210, ~: 31288)
[PASS] test_Increment() (gas: 31303)
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 7.01ms (6.73ms CPU time)

Ran 1 test suite in 8.85ms (7.01ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as forge considers the remapping valid, I'd say to keep the current behaviour, however I'm open to structuring the tests in a different way to make this edge case more obvious

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

structuring the tests in a different way to make this edge case more obvious

Please 🙏🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import '../../../interfaces/ITest.sol';
import './someFile.sol';
`;
import './Contract.sol';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about adding something that should be automatically modified in between these lines? Just to make sure our code is not skipping all the imports after seeing that the first one does not need any modification

No need to add a new test, we could do it in this one

Copy link
Contributor Author

@0xteddybear 0xteddybear Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

now that I think about it (related to your other comment) having multiple imports in a single line will probably break as well (but we could actually make that work without properly parsing the code). Will add a test for that too!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(transformedContent).to.include(`contract C {}`);
});

it.skip('should not have false positives choosing imports', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this test being skipped? we are not able to know if we are talking about code or comment?

Copy link
Contributor Author

@0xteddybear 0xteddybear Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not, since we don't reuse an existing lexer+parser, and only do regular-style modifications to the code. For most cases it is sufficient though, and single-line comments are skipped since we match \s*import to detect imports

Also, its worth noting that the impact of modifying comments when we shouldn't is very low considering multi-line natspec is required to have a leading * which would not match the regex above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xteddybear let's leave a comment to explain why the tests are skipped? I'd also add a warning on the readme.

Copy link
Contributor Author

@0xteddybear 0xteddybear Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the warning in 0b2c27b, however I feel leaving the readme as-is and pinning the issue might be better, wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xteddybear both are fine in my view

@gas1cent gas1cent changed the title Fix: double import issue fix: double import issue Nov 25, 2024
});
});

it('should apply remapping and then remove node_modules from path', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this test a duplicate of should remove node_modules from import paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really since the preconditions are different, the testcase defined in line 48 is only concerned with removing node_modules from import paths, with the precondition of no remapping matching the paths with node_modules in it (expressed as with remapping for local file only)

this testcase, however, ensures that when a remapping matches a path inside node_modules, the remapping is applied before removing node_modules from the path

feedback of course welcome on ways to structure this differently so the above is clear from reading the code alone

@gas1cent gas1cent merged commit beb99c0 into main Nov 27, 2024
3 checks passed
@gas1cent gas1cent deleted the fix/double-import branch November 27, 2024 20:50
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.

3 participants