Skip to content

Commit f297591

Browse files
authored
fix(internal/ensure): Do not fail hard when the submodule does not exist (#736)
Signed-off-by: Kemal Akkoyun <[email protected]> Fixes #732 Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 66658b7 commit f297591

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

internal/ensure/requiredintegrations.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,41 @@ var (
196196

197197
func fetchShippedVersions() map[string]string {
198198
initOnce.Do(func() {
199-
versions := make(map[string]string)
200199
_, thisFile, _, _ := runtime.Caller(0)
201-
// The version of dd-trace-go that shipped with the current version of orchestrion.
202-
// We use this to determine if we need to upgrade dd-trace-go when pinning.
203200
orchestrionRoot := filepath.Join(thisFile, "..", "..", "..")
204-
ver, err := resolveDependencyVersion(orchestrionRoot, integrations.DatadogTracerV2)
205-
if err != nil {
206-
panic(fmt.Errorf("resolving %s version in %q: %w", integrations.DatadogTracerV2, orchestrionRoot, err))
207-
}
208-
versions[integrations.DatadogTracerV2] = ver
209-
instrumentationRoot := filepath.Join(orchestrionRoot, "instrument")
210-
ver, err = resolveDependencyVersion(instrumentationRoot, integrations.DatadogTracerV2All)
211-
if err != nil {
212-
panic(fmt.Errorf("resolving %s version in %q: %w", integrations.DatadogTracerV2All, instrumentationRoot, err))
213-
}
214-
versions[integrations.DatadogTracerV2All] = ver
201+
versions := loadShippedVersions(orchestrionRoot)
215202
orchestrionShippedVersions.Store(&versions)
216203
})
217204
return *orchestrionShippedVersions.Load()
218205
}
206+
207+
func loadShippedVersions(orchestrionRoot string) map[string]string {
208+
versions := make(map[string]string)
209+
210+
// The version of dd-trace-go that shipped with the current version of orchestrion.
211+
// We use this to determine if we need to upgrade dd-trace-go when pinning.
212+
ver, err := resolveDependencyVersion(orchestrionRoot, integrations.DatadogTracerV2)
213+
if err != nil {
214+
panic(fmt.Errorf("resolving %s version in %q: %w", integrations.DatadogTracerV2, orchestrionRoot, err))
215+
}
216+
versions[integrations.DatadogTracerV2] = ver
217+
218+
// Default to using the same version as DatadogTracerV2 for DatadogTracerV2All.
219+
// This serves as a fallback when the instrument directory (which is a separate submodule)
220+
// is not accessible, such as when orchestrion is used as a module dependency from the
221+
// Go module cache where nested submodules may not be present.
222+
versions[integrations.DatadogTracerV2All] = versions[integrations.DatadogTracerV2]
223+
224+
// If the instrument directory exists, override with the actual version from its go.mod
225+
instrumentationRoot := filepath.Join(orchestrionRoot, "instrument")
226+
if _, err := os.Stat(instrumentationRoot); err != nil {
227+
return versions
228+
}
229+
ver, err = resolveDependencyVersion(instrumentationRoot, integrations.DatadogTracerV2All)
230+
if err != nil {
231+
panic(fmt.Errorf("resolving %s version in %q: %w", integrations.DatadogTracerV2All, instrumentationRoot, err))
232+
}
233+
versions[integrations.DatadogTracerV2All] = ver
234+
235+
return versions
236+
}

internal/ensure/requiredintegrations_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"os"
13+
"path/filepath"
1314
"testing"
1415

1516
"github.com/DataDog/orchestrion/internal/gomod"
@@ -411,6 +412,24 @@ func TestFetchShippedVersions(t *testing.T) {
411412
}
412413
}
413414
})
415+
416+
t.Run("fallback-when-instrument-missing", func(t *testing.T) {
417+
// Create temp directory without instrument/ subdirectory
418+
tempDir := t.TempDir()
419+
testGoMod := filepath.Join(tempDir, "go.mod")
420+
require.NoError(t, os.WriteFile(testGoMod, []byte(`module test
421+
require github.com/DataDog/dd-trace-go/v2 v2.3.0
422+
`), 0644))
423+
424+
versions := loadShippedVersions(tempDir)
425+
426+
v2Version := versions["github.com/DataDog/dd-trace-go/v2"]
427+
allVersion := versions["github.com/DataDog/dd-trace-go/orchestrion/all/v2"]
428+
429+
require.NotEmpty(t, v2Version)
430+
require.NotEmpty(t, allVersion)
431+
assert.Equal(t, v2Version, allVersion, "both should use same version when instrument/ is missing")
432+
})
414433
}
415434

416435
func TestRequiredIntegrationsEdgeCases(t *testing.T) {

0 commit comments

Comments
 (0)