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
155 changes: 128 additions & 27 deletions example/producer/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/bazelbuild/rules_go/go/runfiles"
Expand All @@ -15,10 +17,12 @@ import (

func main() {
loadmapPath := flag.String("loadmap", "", "path to the loadmap file")
propertiesPath := flag.String("properties", "", "path to the properties JSON file")
repoName := flag.String("repo", "", "canonical name of the caller's repository")
flag.Parse()

if flag.NArg() != 1 {
fmt.Fprintf(os.Stderr, "usage: %s [--loadmap <path>] <script.star>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "usage: %s --repo <name> [--loadmap <path>] <label>\n", os.Args[0])
os.Exit(1)
}

Expand All @@ -28,31 +32,46 @@ func main() {
os.Exit(1)
}

props, err := parseProperties(*propertiesPath)
if err != nil {
fmt.Fprintf(os.Stderr, "loading properties: %v\n", err)
os.Exit(1)
}

predeclared := starlark.StringDict{
"read_file": starlark.NewBuiltin("read_file", readFile),
"read_file": starlark.NewBuiltin("read_file", readFile),
"get_property": starlark.NewBuiltin("get_property", makeGetProperty(props)),
}

opts := &syntax.FileOptions{
TopLevelControl: true,
GlobalReassign: true,
}
ldr := &loader{
repoMap: repoMap,
cache: map[string]*cacheEntry{},
predeclared: predeclared,
opts: opts,
repo: *repoName,
repoMap: repoMap,
cache: map[string]*cacheEntry{},
predeclared: predeclared,
opts: opts,
pathToModule: map[string]moduleLocation{},
}

entryLabel := flag.Arg(0)
rloc, err := ldr.resolveLabel(entryLabel)
if err != nil {
fmt.Fprintf(os.Stderr, "resolving entrypoint %q: %v\n", entryLabel, err)
os.Exit(1)
}

entryRloc := flag.Arg(0)
absPath, err := runfiles.Rlocation(entryRloc)
realPath, err := ldr.registerFile(rloc)
if err != nil {
fmt.Fprintf(os.Stderr, "resolving runfile %q: %v\n", entryRloc, err)
fmt.Fprintf(os.Stderr, "loading entrypoint %q: %v\n", entryLabel, err)
os.Exit(1)
}

src, err := os.ReadFile(absPath)
src, err := os.ReadFile(realPath)
if err != nil {
fmt.Fprintf(os.Stderr, "reading %q: %v\n", absPath, err)
fmt.Fprintf(os.Stderr, "reading %q: %v\n", realPath, err)
os.Exit(1)
}

Expand All @@ -64,7 +83,7 @@ func main() {
},
}

_, err = starlark.ExecFileOptions(opts, thread, entryRloc, src, predeclared)
_, err = starlark.ExecFileOptions(opts, thread, realPath, src, predeclared)
if err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
fmt.Fprintln(os.Stderr, evalErr.Backtrace())
Expand All @@ -75,18 +94,44 @@ func main() {
}
}

type moduleLocation struct {
repo string // canonical repo name (first path component of the rlocation path)
rpath string // path within the repo
}

type loader struct {
repoMap map[string]string
cache map[string]*cacheEntry
predeclared starlark.StringDict
opts *syntax.FileOptions
repo string // canonical name of the caller's repository
repoMap map[string]string
cache map[string]*cacheEntry
predeclared starlark.StringDict
opts *syntax.FileOptions
pathToModule map[string]moduleLocation
}

type cacheEntry struct {
globals starlark.StringDict
err error
}

// registerFile resolves an rlocation path to a real filesystem path,
// records the reverse mapping, and returns the real path.
func (l *loader) registerFile(rloc string) (string, error) {
absPath, err := runfiles.Rlocation(rloc)
if err != nil {
return "", fmt.Errorf("resolving runfile %q: %w", rloc, err)
}
realPath, err := filepath.EvalSymlinks(absPath)
if err != nil {
return "", fmt.Errorf("resolving symlinks for %q: %w", absPath, err)
}
repo := repoOf(rloc)
l.pathToModule[realPath] = moduleLocation{
repo: repo,
rpath: rloc[len(repo)+1:],
}
return realPath, nil
}

func (l *loader) load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
callerFile := thread.CallStack().At(0).Pos.Filename()

Expand All @@ -99,14 +144,14 @@ func (l *loader) load(thread *starlark.Thread, module string) (starlark.StringDi
return entry.globals, entry.err
}

absPath, err := runfiles.Rlocation(rloc)
realPath, err := l.registerFile(rloc)
if err != nil {
return nil, fmt.Errorf("resolving %q: %w", rloc, err)
return nil, err
}

src, err := os.ReadFile(absPath)
src, err := os.ReadFile(realPath)
if err != nil {
return nil, fmt.Errorf("reading %q: %w", absPath, err)
return nil, fmt.Errorf("reading %q: %w", realPath, err)
}

child := &starlark.Thread{
Expand All @@ -117,23 +162,38 @@ func (l *loader) load(thread *starlark.Thread, module string) (starlark.StringDi
},
}

globals, execErr := starlark.ExecFileOptions(l.opts, child, rloc, src, l.predeclared)
globals, execErr := starlark.ExecFileOptions(l.opts, child, realPath, src, l.predeclared)
l.cache[rloc] = &cacheEntry{globals, execErr}
return globals, execErr
}

func (l *loader) resolve(callerRloc, module string) (string, error) {
func (l *loader) resolve(callerFile, module string) (string, error) {
if strings.HasPrefix(module, "@") {
return l.resolveExternal(module)
}

loc, ok := l.pathToModule[callerFile]
if !ok {
return "", fmt.Errorf("unknown caller %q: not tracked in module map", callerFile)
}

if strings.HasPrefix(module, "//") {
repo := repoOf(callerRloc)
return resolveRepoRelative(repo, module)
return resolveRepoRelative(loc.repo, module)
}
if strings.HasPrefix(module, ":") {
return path.Dir(callerRloc) + "/" + module[1:], nil

module = strings.TrimPrefix(module, ":")
return loc.repo + "/" + path.Join(path.Dir(loc.rpath), module), nil
}

// resolveLabel resolves an absolute starlark label to an rlocation path.
func (l *loader) resolveLabel(label string) (string, error) {
if strings.HasPrefix(label, "//") {
return resolveRepoRelative(l.repo, label)
}
return path.Dir(callerRloc) + "/" + module, nil
if !strings.HasPrefix(label, "@") {
return "", fmt.Errorf("invalid label %q: must start with @ or //", label)
}
return l.resolveExternal(label)
}

func (l *loader) resolveExternal(module string) (string, error) {
Expand Down Expand Up @@ -207,6 +267,47 @@ func parseLoadmap(loadmapRloc string) (map[string]string, error) {
return m, scanner.Err()
}

func parseProperties(propsRloc string) (map[string]string, error) {
if propsRloc == "" {
return map[string]string{}, nil
}

absPath, err := runfiles.Rlocation(propsRloc)
if err != nil {
return nil, fmt.Errorf("resolving properties %q: %w", propsRloc, err)
}

data, err := os.ReadFile(absPath)
if err != nil {
return nil, err
}

var m map[string]string
if err := json.Unmarshal(data, &m); err != nil {
return nil, fmt.Errorf("parsing properties JSON: %w", err)
}
return m, nil
}

func makeGetProperty(props map[string]string) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
return func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name string
var defaultValue starlark.Value
if err := starlark.UnpackArgs("get_property", args, kwargs, "name", &name, "default?", &defaultValue); err != nil {
return nil, err
}

val, ok := props[name]
if !ok {
if defaultValue != nil {
return defaultValue, nil
}
return nil, fmt.Errorf("get_property: unknown property %q", name)
}
return starlark.String(val), nil
}
}

func readFile(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var p string
if err := starlark.UnpackArgs("read_file", args, kwargs, "path", &p); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def _local_starlark_repository_impl(rctx):
rctx.watch(repo_root)
rctx.symlink(repo_root, ".")


local_starlark_repository = repository_rule(
implementation = _local_starlark_repository_impl,
attrs = {"repo_file": attr.label(
Expand Down
52 changes: 45 additions & 7 deletions example/producer/rules/starlark_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ load("@rules_runfiles_group//runfiles_group:lib.bzl", "lib")
load("@rules_runfiles_group//runfiles_group:providers.bzl", "RunfilesGroupInfo", "RunfilesGroupSelectionInfo")
load("//producer/providers:providers.bzl", "StarlarkInfo")

def _canonical_repo_name(ctx):
return ctx.label.repo_name or "_main"

def _starlark_binary_impl(ctx):
interpreter_info = ctx.attr.interpreter[DefaultInfo]
interpreter_exe = interpreter_info.files_to_run.executable
entrypoint = ctx.file.src
current_repo = _canonical_repo_name(ctx)

# Collect repos from all deps + self + standard library
transitive_repos = [dep[StarlarkInfo].repos for dep in ctx.attr.deps]
stdlib = ctx.attr._standard_library
all_repos = depset(
[
(ctx.attr.repository, ctx.label.repo_name),
("std", stdlib.label.repo_name),
(ctx.attr.repository, current_repo),
("std", stdlib.label.repo_name or "_main"),
],
transitive = transitive_repos,
)
Expand All @@ -38,8 +42,30 @@ def _starlark_binary_impl(ctx):
progress_message = "Generating loadmap for %{label}",
)

# Build launcher stub: interpreter --loadmap <loadmap> <entrypoint>
# Write properties file
properties = ctx.actions.declare_file(ctx.label.name + ".properties.json")
expanded_props = {}
for k, v in ctx.attr.properties.items():
expanded_props[k] = ctx.expand_location(v, ctx.attr.data)
ctx.actions.write(properties, json.encode(expanded_props))

# Build launcher stub: interpreter --repo <repo> --loadmap <loadmap> --properties <props> <entrypoint_label>
if ctx.attr.repository:
entry_label = "@" + ctx.attr.repository + "//" + entrypoint.owner.package + ":" + entrypoint.owner.name
else:
entry_label = "//" + entrypoint.owner.package + ":" + entrypoint.owner.name

embedded_args, transformed_args = launcher.args_from_entrypoint(interpreter_exe)
embedded_args, transformed_args = launcher.append_embedded_arg(
arg = "--repo",
embedded_args = embedded_args,
transformed_args = transformed_args,
)
embedded_args, transformed_args = launcher.append_embedded_arg(
arg = current_repo,
embedded_args = embedded_args,
transformed_args = transformed_args,
)
embedded_args, transformed_args = launcher.append_embedded_arg(
arg = "--loadmap",
embedded_args = embedded_args,
Expand All @@ -50,8 +76,18 @@ def _starlark_binary_impl(ctx):
embedded_args = embedded_args,
transformed_args = transformed_args,
)
embedded_args, transformed_args = launcher.append_embedded_arg(
arg = "--properties",
embedded_args = embedded_args,
transformed_args = transformed_args,
)
embedded_args, transformed_args = launcher.append_runfile(
file = entrypoint,
file = properties,
embedded_args = embedded_args,
transformed_args = transformed_args,
)
embedded_args, transformed_args = launcher.append_embedded_arg(
arg = entry_label,
embedded_args = embedded_args,
transformed_args = transformed_args,
)
Expand All @@ -66,7 +102,7 @@ def _starlark_binary_impl(ctx):
)

# Runfiles: interpreter + entrypoint + loadmap + stdlib + data + all deps
runfiles = ctx.runfiles(files = [entrypoint, loadmap] + ctx.files.data)
runfiles = ctx.runfiles(files = [entrypoint, loadmap, properties] + ctx.files.data)
runfiles = runfiles.merge(interpreter_info.default_runfiles)
runfiles = runfiles.merge(stdlib[DefaultInfo].default_runfiles)
for dep in ctx.attr.deps:
Expand Down Expand Up @@ -94,10 +130,9 @@ def _starlark_binary_impl(ctx):
# Special group: std
groups["std"] = stdlib[DefaultInfo].default_runfiles.files

entrypoint_files = depset([output, entrypoint, loadmap] + ctx.files.data)
entrypoint_files = depset([output, entrypoint, loadmap, properties] + ctx.files.data)

# Dep groups
current_repo = ctx.label.repo_name
if ctx.attr.runfiles_grouping == "by_target":
# Keep a separate entrypoint group in by_target mode.
groups["entrypoint"] = entrypoint_files
Expand Down Expand Up @@ -176,6 +211,9 @@ starlark_binary = rule(
allow_files = True,
doc = "Data files available at runtime.",
),
"properties": attr.string_dict(
doc = "Key-value properties accessible via get_property() at runtime. Values support $(location) expansion.",
),
"interpreter": attr.label(
default = Label("//producer/interpreter"),
executable = True,
Expand Down
6 changes: 5 additions & 1 deletion example/producer/rules/starlark_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ load("@rules_runfiles_group//runfiles_group:lib.bzl", "lib")
load("@rules_runfiles_group//runfiles_group:providers.bzl", "RunfilesGroupInfo")
load("//producer/providers:providers.bzl", "StarlarkInfo")

def _canonical_repo_name(ctx):
return ctx.label.repo_name or "_main"

def _starlark_library_impl(ctx):
direct_srcs = ctx.files.srcs

transitive_sources = [dep[StarlarkInfo].sources for dep in ctx.attr.deps]
all_sources = depset(direct_srcs, transitive = transitive_sources)

transitive_repos = [dep[StarlarkInfo].repos for dep in ctx.attr.deps]
repos = depset([(ctx.attr.repository, ctx.label.repo_name)], transitive = transitive_repos)
current_repo = _canonical_repo_name(ctx)
repos = depset([(ctx.attr.repository, current_repo)], transitive = transitive_repos)

if ctx.attr.repository:
loadpath = "@" + ctx.attr.repository + "//" + ctx.label.package
Expand Down
Loading
Loading