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

Bug(forge build): ambiguity in source unit IDs with incremental builds affects source maps #6067

Closed
2 tasks done
RaoulSchaffranek opened this issue Oct 19, 2023 · 2 comments
Closed
2 tasks done
Labels
C-forge Command: forge Cmd-forge-build Command: forge build T-bug Type: bug T-to-investigate Type: to investigate

Comments

@RaoulSchaffranek
Copy link

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (490b588 2023-10-19T00:17:56.537546457Z)

What command(s) is the bug in?

forge build

Operating System

Linux

Describe the bug

I observed that source maps exhibit some ambiguity in relation to their source unit IDs during incremental builds. Specifically, upon building a project and adding a new contract within a new file, followed by another build, the new file incorrectly receives a source unit ID of 0 already utilized. Consequently, multiple source units now share an identical ID, creating confusion about which file certain source unit entries within the source maps correspond.

Steps to Reproduce

  1. Initialize a new contract file under src/First.sol with the ensuing content:

    // SPDX-License-Identifier: UNLICENSED
    pragma solidity ^0.8.21;
    
    contract First {
    }
  2. Execute the command forge build.

  3. Subsequently, create an additional contract within src/Second.sol including the following:

    // SPDX-License-Identifier: UNLICENSED
    pragma solidity ^0.8.21;
    
    contract Second {
    }
  4. Run the command forge build once again.

  5. Proceed by examining the generated output artifacts, specifically out/First.sol/First.json and out/Second.sol/Second.json, noting that the content is abbreviated for brevity. The contents are as follows:

    // First.json
    {
      "bytecode": {
        "sourceMap": "65:18:0:-:0;;;;;;;;;;;;;;;;;;;"
      },
      "deployedBytecode": {
        "sourceMap": "65:18:0:-:0;;;;;"
      },
      "id": 0
    }
    
    // Second.json
    {
      "bytecode": {
        "sourceMap": "65:19:0:-:0;;;;;;;;;;;;;;;;;;;",
      },
      "deployedBytecode": {
        "sourceMap": "65:19:0:-:0;;;;;",
      },
      "id": 0
    }

Current Behavior

The artifacts generated post-build, namely First.json and Second.json, both possess an id attribute at the top level with a value of 0. This id ostensibly represents the source unit ID. However, the source maps within each artifact refer to this identical ID, leading to an ambiguity as to the actual source unit to which the source maps are referring.

Expected Behavior

Each unique source unit or file should be assigned a distinct source unit ID to prevent overlaps, thereby eliminating any ambiguity in the source maps. This clear distinction is vital for ensuring the source maps accurately reflect their corresponding source units, especially in incremental build environments.

@RaoulSchaffranek RaoulSchaffranek added the T-bug Type: bug label Oct 19, 2023
@gakonst gakonst added this to Foundry Oct 19, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Oct 19, 2023
@zerosnacks zerosnacks changed the title Ambiguity in Source Unit IDs with Incremental Builds Affects Source Maps Bug(forge build): ambiguity in source unit IDs with incremental builds affects source maps Jul 4, 2024
@zerosnacks zerosnacks added C-forge Command: forge Cmd-forge-build Command: forge build T-to-investigate Type: to investigate labels Jul 4, 2024
@zerosnacks
Copy link
Member

zerosnacks commented Jul 4, 2024

Able to reproduce with given setup, cc @klkvr

@klkvr
Copy link
Member

klkvr commented Jul 4, 2024

after foundry-rs/compilers#140 we keep a mapping from build_id to source ids allowing to determine which source given id in source map corresponds to.

It is already used in debugger:

pub struct ContractSources {

There's not much we can do to ensure unique ids for incremental builds as IDs are getting assigned by solc

@klkvr klkvr closed this as completed Jul 4, 2024
@jenpaff jenpaff moved this from Todo to Completed in Foundry Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-build Command: forge build T-bug Type: bug T-to-investigate Type: to investigate
Projects
Archived in project
Development

No branches or pull requests

3 participants