func()

in tool/preprocess/preprocess.go [654:700]


func (dp *DepProcessor) rectifyMod() error {
	// Backup go.mod and go.sum files
	gomodDir := dp.getGoModDir()
	files := []string{}
	files = append(files, filepath.Join(gomodDir, util.GoModFile))
	files = append(files, filepath.Join(gomodDir, util.GoSumFile))
	files = append(files, filepath.Join(gomodDir, util.GoWorkSumFile))
	for _, file := range files {
		if util.PathExists(file) {
			err := dp.backupFile(file)
			if err != nil {
				return err
			}
		}
	}
	// Since we haven't published the alibaba-otel pkg module, we need to add
	// a replace directive to tell the go tool to use the local module cache
	// instead of the remote module. This is a workaround for the case that
	// the remote module is not available(published).
	gomod := dp.getGoModPath()
	modfile, err := parseGoMod(gomod)
	if err != nil {
		return err
	}
	hasReplace := false
	for _, r := range modfile.Replace {
		if r.Old.Path == pkgPrefix {
			hasReplace = true
			break
		}
	}
	if !hasReplace {
		err = modfile.AddReplace(pkgPrefix, "", dp.pkgLocalCache, "")
		if err != nil {
			return err
		}
		bs, err := modfile.Format()
		if err != nil {
			return err
		}
		_, err = util.WriteFile(gomod, string(bs))
		if err != nil {
			return err
		}
	}
	return nil
}