Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions desktop/internal/toolchain/environment_link_other_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//go:build !windows

package toolchain

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
)

// runtimeManifestLinkingOutside is the fixture runtime whose definition lets
// its program link outside the home.
func runtimeManifestLinkingOutside(t *testing.T) json.RawMessage {
t.Helper()
var manifest map[string]any
if err := json.Unmarshal(readManifest(t, "runtime.plugin.json"), &manifest); err != nil {
t.Fatal(err)
}
definition := manifest["toolDefinitions"].([]any)[0].(map[string]any)
definition["executableLinksOutsideHome"] = true
encoded, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
return encoded
}

// A virtual environment's program links to its base installation outside the
// environment. Core accepts it only for a definition that allows it and only
// when the host recorded every directory on the layout path, here jre and
// jre/bin. The program is probed and started from the environment's layout
// path, never its target, so it still finds the environment it belongs to.
func TestAnEnvironmentProgramLinkedOutsideItsHomeStartsFromItsLayoutPath(t *testing.T) {
for _, allowed := range []bool{true, false} {
name := "definition-allows-it"
if !allowed {
name = "definition-keeps-the-program-inside"
}
t.Run(name, func(t *testing.T) {
root, home, base, environment := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir()
baseProgram := filepath.Join(base, "jre", "bin", "vm")
writeExecutable(t, baseProgram)
bin := filepath.Join(environment, "jre", "bin")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(baseProgram, filepath.Join(bin, "vm")); err != nil {
t.Fatal(err)
}
canonicalEnvironment := canonical(t, environment)
program := filepath.Join(canonicalEnvironment, "jre", "bin", "vm")
// Only the layout path answers: probing the base program would
// leave the environment.
options := fixtureOptions(home, environment, map[string]string{program: "vm 7.1\n"})
manifest := readManifest(t, "runtime.plugin.json")
if allowed {
manifest = runtimeManifestLinkingOutside(t)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
snapshot, err := Load(ctx, core.NewClient(), root, ".", []json.RawMessage{manifest}, options)
if err != nil {
t.Fatal(err)
}
if len(snapshot.Tools) != 1 {
t.Fatalf("tools = %+v", snapshot.Tools)
}
effective := snapshot.Tools[0].Effective
if !allowed {
if effective != nil {
t.Fatalf("selected %s although the definition keeps its program inside the home", effective.Executable)
}
return
}
if effective == nil {
for _, trace := range snapshot.Tools[0].Trace {
if trace.Reason != nil {
t.Logf("trace reason = %s", trace.Reason.Code)
}
}
t.Fatal("the environment was not selected")
}
if filepath.Clean(effective.Executable) != program || effective.Version != "7.1" {
t.Fatalf("selection = %s %s, want %s 7.1", effective.Executable, effective.Version, program)
}
if effective.Home == nil || filepath.Clean(*effective.Home) != canonicalEnvironment {
t.Fatalf("home = %v, want %s", effective.Home, canonicalEnvironment)
}
if got := effective.Environment["FICTIONAL_VM_HOME"]; filepath.Clean(got) != canonicalEnvironment {
t.Fatalf("FICTIONAL_VM_HOME = %s, want %s", got, canonicalEnvironment)
}
})
}
}
6 changes: 5 additions & 1 deletion desktop/internal/toolchain/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,15 @@ func providerAbsolutePath(path string) bool {
filepath.IsAbs(path) && filepath.Clean(path) == filepath.FromSlash(path)
}

// observeToolHome records the facts Core needs to check an external home: the
// home, every directory on each layout path and the program with its link target,
// and the PATH directories. A program that links outside its home is accepted only
// when Core sees each directory on the way stay inside the home.
func (w *workspace) observeToolHome(home string, definition core.ToolchainToolDefinition) {
w.observe(home)
for _, layout := range definition.ExecutableLayouts {
if platformMatches(layout.Platforms, w.platform) {
w.observe(filepath.Join(home, filepath.FromSlash(layout.Path)))
w.observeChain(home, filepath.Join(home, filepath.FromSlash(layout.Path)))
}
}
for _, template := range definition.PathPrepend {
Expand Down
6 changes: 5 additions & 1 deletion scripts/desktop/build-desktop-core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ PINNED_CORE_REPOSITORY="https://github.com/Lithe-IDEA/Lithe-core-rust.git"
# context; #174 merged with that field under a new ui 1, and #175 makes choose an optional field
# within ui 0 again. The sources are the same as the branch commit a50cea1 that the desktop
# pinned before; only the protocol changelog differs.
PINNED_CORE_REVISION="dcb3d77f7a56e58f3d7c0aceea5b7df6986ed2e2"
# Lithe-core-rust branch feat/toolchain-leaf-links-outside-home at 33e6b60 (#177, not merged yet):
# a tool definition may set executableLinksOutsideHome, a toolDefinitions 0 addition, so a
# virtual environment's program that links to its base interpreter outside the home is accepted
# and started from its layout path; the directories on the way must stay inside the home.
PINNED_CORE_REVISION="33e6b601857e22a787e1a4ce26da2a24a1ce41ea"
PROFILE="debug"
CHECKOUT_ONLY="no"
while [[ $# -gt 0 ]]; do
Expand Down
Loading
Loading