diff --git a/pyodide_build/cli/xbuildenv.py b/pyodide_build/cli/xbuildenv.py index ab1806f0..230b86ab 100644 --- a/pyodide_build/cli/xbuildenv.py +++ b/pyodide_build/cli/xbuildenv.py @@ -232,4 +232,3 @@ def _install_emscripten( emsdk_dir = manager.install_emscripten(version) print("Installing emsdk complete.") - print(f"Use `source {emsdk_dir}/emsdk_env.sh` to set up the environment.") diff --git a/pyodide_build/tests/test_cli_install_emscripten.py b/pyodide_build/tests/test_cli_install_emscripten.py index 78674176..12e9685e 100644 --- a/pyodide_build/tests/test_cli_install_emscripten.py +++ b/pyodide_build/tests/test_cli_install_emscripten.py @@ -60,8 +60,6 @@ def fake_install(self, version): assert result.exit_code == 0, result.stdout assert "Installing emsdk..." in result.stdout, result.stdout assert "Installing emsdk complete." in result.stdout, result.stdout - assert "Use `source" in result.stdout, result.stdout - assert "emsdk_env.sh` to set up the environment." in result.stdout, result.stdout assert called["version"] == "3.1.46" @@ -134,7 +132,6 @@ def fake_install(self, version): assert result.exit_code == 0, result.stdout assert "Installing emsdk..." in result.stdout, result.stdout assert "Installing emsdk complete." in result.stdout, result.stdout - assert str(existing_emsdk / "emsdk_env.sh") in result.stdout def test_install_emscripten_git_failure(tmp_path, monkeypatch): @@ -220,5 +217,3 @@ def test_install_emscripten_output_format(tmp_path, monkeypatch): # Verify output format - check for key messages (logger adds extra lines) assert "Installing emsdk..." in result.stdout assert "Installing emsdk complete." in result.stdout - assert "Use `source" in result.stdout - assert "emsdk_env.sh` to set up the environment." in result.stdout diff --git a/pyodide_build/tests/test_install_emscripten.py b/pyodide_build/tests/test_install_emscripten.py index d0054476..dd1ab6d0 100644 --- a/pyodide_build/tests/test_install_emscripten.py +++ b/pyodide_build/tests/test_install_emscripten.py @@ -113,9 +113,9 @@ def mock_run_side_effect(cmd, **kwargs): # Verify assert result == emsdk_dir - assert mock_run.call_count == 4 # clone + install + patch + activate + assert mock_run.call_count == 5 # clone + install + patch + activate + source env - # Check the four subprocess calls + # Check the subprocess calls in the expected order calls = mock_run.call_args_list # 1. Clone emsdk @@ -153,6 +153,14 @@ def mock_run_side_effect(cmd, **kwargs): check=True, ) + # 5. Source the environment script to validate activation + assert calls[4] == call( + "source ./emsdk_env.sh", + check=True, + shell=True, + cwd=emsdk_dir, + ) + def test_install_emscripten_specific_version(tmp_path, monkeypatch): """Test installing a specific Emscripten SDK version""" @@ -182,7 +190,7 @@ def mock_run_side_effect(cmd, **kwargs): # Verify assert result == emsdk_dir - assert mock_run.call_count == 4 # clone + install + patch + activate + assert mock_run.call_count == 5 # clone + install + patch + activate + source env calls = mock_run.call_args_list @@ -208,6 +216,14 @@ def mock_run_side_effect(cmd, **kwargs): check=True, ) + # Verify sourcing of the environment script (call 4) + assert calls[4] == call( + "source ./emsdk_env.sh", + check=True, + shell=True, + cwd=emsdk_dir, + ) + def test_install_emscripten_with_existing_emsdk(tmp_path, monkeypatch): """Test installing Emscripten when emsdk directory already exists""" @@ -234,7 +250,7 @@ def test_install_emscripten_with_existing_emsdk(tmp_path, monkeypatch): # Verify - should pull, then install, patch, and activate assert result == emsdk_dir - assert mock_run.call_count == 4 + assert mock_run.call_count == 5 calls = mock_run.call_args_list @@ -261,6 +277,14 @@ def test_install_emscripten_with_existing_emsdk(tmp_path, monkeypatch): check=True, ) + # 5. Source the environment script + assert calls[4] == call( + "source ./emsdk_env.sh", + check=True, + shell=True, + cwd=emsdk_dir, + ) + def test_install_emscripten_patch_fails(tmp_path, monkeypatch): """Test handling of patch application failure due to version mismatch""" diff --git a/pyodide_build/xbuildenv.py b/pyodide_build/xbuildenv.py index 745172e9..a9a71144 100644 --- a/pyodide_build/xbuildenv.py +++ b/pyodide_build/xbuildenv.py @@ -520,6 +520,19 @@ def install_emscripten(self, emscripten_version: str = "latest") -> Path: ) logger.info("Emscripten SDK installed successfully at %s", emsdk_dir) + + try: + subprocess.run( + "source ./emsdk_env.sh", + check=True, + shell=True, + cwd=emsdk_dir, + ) + except subprocess.CalledProcessError as e: + raise RuntimeError( + f"Failed to activate emsdk environment. Please check the emsdk installation at {emsdk_dir}" + ) from e + return emsdk_dir def _add_version_marker(self) -> None: