Skip to content

Modifying a module's imported packages? #1016

@SolarLune

Description

@SolarLune

Hey, there - I'm trying to use Scriggo to build and run a Go module. The main.go imports a package in a subdirectory, but I want to substitute the declarations of that imported package prior to building - I think this isn't working currently.

For example, in the below test code, it should load a directory (subDir) that has a go.mod and main.go in its root (under the scripts directory), and then substitute the object Definitions defined in scripts/interop with the map in baseTestMain.go. Instead, nothing happens and no error is printed, which makes me think scriggo.Build() is creating a new, blank package instead of using the package I'm defining below.

assets/scripts/go.mod

module scripts

go 1.25.0

assets/scripts/main.go

package main

import "scripts/interop"

func main() {
	println("Run?")

	interop.Definitions["Test"] = 100

}

baseTestMain.go

	t := map[string]int{}

    // packages is defined earlier, has `fmt` imported

	packages := native.Packages{
		"scripts/interop": native.Package{
			Name: "interop",
			Declarations: native.Declarations{
				"Definitions":          &t,
			},
		},
	}

     // subDir here is a FS that has a root at `assets/scripts`
	prog, err := scriggo.Build(subDir, &scriggo.BuildOptions{
		Packages: packages,
	})

	err = prog.Run(&scriggo.RunOptions{})

	fmt.Println(t) // Should print map["Test": 100], instead just prints map[] 

Can someone confirm if I have the right concept on how this should work with modules?

EDIT: For clarity on what I'm asking and why, I'm making a game in Go and trying to see if I can use Scriggo as a scripting layer. I've tried this before, but it's been awhile. I'd like the user to be able to mod the game, so I'd like the game to have scripts run from assets/scripts. A file at assets/scripts/interop/interop.go would be essentially a connective layer that provides code completion, with the scriggo.Build function substituting key elements from that interop layer with things it can read from / write to, the "real" package.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions