Skip to content

Commit 8544a9d

Browse files
authored
fix: local replace will produce invalid require (#17)
1 parent 69e9adf commit 8544a9d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lang/golang/writer/write.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type chunk struct {
5757
line int
5858
}
5959

60+
const localVersion = "v0.0.0"
61+
6062
func NewWriter(opts Options) *Writer {
6163
if opts.CompilerPath == "" {
6264
opts.CompilerPath = "go"
@@ -143,20 +145,37 @@ func (w *Writer) WriteModule(repo *uniast.Repository, modPath string, outDir str
143145
}
144146
bs.WriteString(goVersion)
145147
bs.WriteString("\n\n")
148+
149+
replaces := make(map[string]string)
146150
if len(mod.Dependencies) > 0 {
147151
bs.WriteString("require (\n")
148152
for name, dep := range mod.Dependencies {
149153
bs.WriteString("\t")
150154
bs.WriteString(name)
151155
sp := strings.Split(dep, "@")
152156
if len(sp) == 2 {
153-
bs.WriteString(" ")
154-
bs.WriteString(sp[1])
157+
if sp[1] == "" {
158+
bs.WriteString(" ")
159+
bs.WriteString(localVersion)
160+
replaces[name] = sp[0]
161+
} else {
162+
bs.WriteString(" ")
163+
bs.WriteString(sp[1])
164+
}
155165
}
156166
bs.WriteString("\n")
157167
}
158168
bs.WriteString(")\n\n")
159169
}
170+
171+
for name, dep := range replaces {
172+
bs.WriteString("replace ")
173+
bs.WriteString(name)
174+
bs.WriteString(" => ")
175+
bs.WriteString(dep)
176+
bs.WriteString("\n")
177+
}
178+
160179
if err := os.WriteFile(filepath.Join(outdir, "go.mod"), []byte(bs.String()), 0644); err != nil {
161180
return fmt.Errorf("write go.mod failed: %v", err)
162181
}

0 commit comments

Comments
 (0)