Skip to content

Bugfix: path dependencies module resolution bug with local paths #4563

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

daniellionel01
Copy link

Code to fix #2278

I hope this is an acceptable solution, I've inspected closed PRs that didn't make it attempting to fix this issue (#2334 and #3398).

In summary this approach stores a hash of the manifest.toml of all dependencies that are a path dependencies in the build directory and rebuilds any packages if the manifest.toml should change.

Let me know if I've missed anything or if this approach does not scale well, thank you!

Also I did not want to clutter the codebase with it, but here is a script (thank you GPT) to test the scenario that produces the error mentioned in the linked issue. It assumes you're running it in the root of the gleam codebase, otherwise you can adjust the GLEAM_BIN path. Also this script probably only works on a mac or linux. Here is the script:

#!/bin/bash
set -e

# Use the locally built gleam executable
GLEAM_BIN="$PWD/target/debug/gleam"

# Ensure the local build exists
if [ ! -f "$GLEAM_BIN" ]; then
    echo "Error: Local Gleam build not found at $GLEAM_BIN"
    echo "Please run 'cargo build' first"
    exit 1
fi

echo "Using Gleam binary at: $GLEAM_BIN"

# Create a temporary directory for the test
TEST_DIR=$(mktemp -d)
echo "Creating test environment in $TEST_DIR"

# Create bar (dependency) project
echo "Creating bar project (the dependency)"
cd $TEST_DIR
mkdir -p bar
cd bar

# Initialize bar project
cat > gleam.toml << EOF
name = "bar"
version = "0.1.0"

[dependencies]
gleam_stdlib = "~> 0.29"
EOF

# Create bar module
mkdir -p src
cat > src/bar.gleam << EOF
import gleam/io

pub fn say_hello() {
  io.println("Hello from bar!")
}
EOF

# Create foo (root) project
echo "Creating foo project (the root project)"
cd $TEST_DIR
mkdir -p foo
cd foo

# Initialize foo project with bar as path dependency
cat > gleam.toml << EOF
name = "foo"
version = "0.1.0"

[dependencies]
bar = { path = "../bar" }
EOF

# Create foo module that imports bar
mkdir -p src
cat > src/foo.gleam << EOF
import bar

pub fn main() {
  bar.say_hello()
}
EOF

# First build of foo
echo "Building foo for the first time..."
cd $TEST_DIR/foo
$GLEAM_BIN build

# Add simplifile dependency to bar
echo "Adding simplifile dependency to bar..."
cd $TEST_DIR/bar
$GLEAM_BIN add simplifile

# Update bar module to use simplifile
cat > src/bar.gleam << EOF
import simplifile
import gleam/io

pub fn say_hello() {
  io.println("Hello from bar!")
}
EOF

# Build foo again - this should now succeed with our fix
echo "Building foo again after adding simplifile to bar..."
cd $TEST_DIR/foo
$GLEAM_BIN build

if [ $? -eq 0 ]; then
  echo -e "\n\033[0;32mSUCCESS: The fix works! Foo successfully built after bar added simplifile dependency.\033[0m"
else
  echo -e "\n\033[0;31mFAILURE: The fix didn't work. Foo failed to build after bar added simplifile dependency.\033[0m"
  exit 1
fi

# Clean up
echo -e "\nTest directory: $TEST_DIR"
echo "You can safely remove this directory when done with: rm -rf $TEST_DIR"
echo "Or inspect it to see the project structure."

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.

Path dependencies module resolution bug
1 participant