From 16d3eeeb8cca75c737bb5c334483c2c37d051e32 Mon Sep 17 00:00:00 2001 From: Titus Fortner Date: Sun, 18 Jan 2026 23:45:37 -0600 Subject: [PATCH 1/2] [build] fix bazel nuget push implementation --- dotnet/private/nuget_push.bzl | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/dotnet/private/nuget_push.bzl b/dotnet/private/nuget_push.bzl index 8182d1f6cb25a..13152e1773bd9 100644 --- a/dotnet/private/nuget_push.bzl +++ b/dotnet/private/nuget_push.bzl @@ -13,7 +13,8 @@ def _nuget_push_impl(ctx): else: script = _create_unix_script(ctx, dotnet, nupkg_files) - runfiles = ctx.runfiles(files = nupkg_files).merge(toolchain.runtime.default_runfiles) + runfiles = ctx.runfiles(files = nupkg_files + [dotnet]) + runfiles = runfiles.merge(toolchain.runtime.default_runfiles) return [ DefaultInfo( @@ -27,15 +28,28 @@ def _create_unix_script(ctx, dotnet, nupkg_files): push_commands = [] for nupkg in nupkg_files: push_commands.append( - '"$DOTNET" nuget push "%s" --api-key "$NUGET_API_KEY" --source "$NUGET_SOURCE"' % nupkg.short_path, + '"$DOTNET" nuget push "$RUNFILES_DIR/_main/{nupkg}" --api-key "$NUGET_API_KEY" --source "$NUGET_SOURCE" --skip-duplicate --no-symbols'.format(nupkg = nupkg.short_path), ) + # External repos in bzlmod have paths like ../repo_name/path, which in runfiles becomes repo_name/path + dotnet_runfiles_path = dotnet.short_path.lstrip("../") + script_content = """#!/usr/bin/env bash set -euo pipefail -DOTNET="$(cd "$(dirname "$0")" && pwd)/{dotnet}" + +# Locate runfiles directory +if [[ -d "$0.runfiles/_main" ]]; then + RUNFILES_DIR="$0.runfiles" +elif [[ -n "${{RUNFILES_DIR:-}}" ]]; then + RUNFILES_DIR="$RUNFILES_DIR" +else + RUNFILES_DIR="$(cd "$(dirname "$0")" && pwd)" +fi + +DOTNET="$RUNFILES_DIR/{dotnet}" {push_commands} """.format( - dotnet = dotnet.short_path, + dotnet = dotnet_runfiles_path, push_commands = "\n".join(push_commands), ) @@ -53,7 +67,7 @@ def _create_windows_script(ctx, dotnet, nupkg_files): for nupkg in nupkg_files: nupkg_path = nupkg.short_path.replace("/", "\\") push_commands.append( - '"%%DOTNET%%" nuget push "%s" --api-key "%%NUGET_API_KEY%%" --source "%%NUGET_SOURCE%%"' % nupkg_path, + '"%%DOTNET%%" nuget push "%s" --api-key "%%NUGET_API_KEY%%" --source "%%NUGET_SOURCE%%" --skip-duplicate --no-symbols' % nupkg_path, ) push_commands.append("if %%ERRORLEVEL%% neq 0 exit /b %%ERRORLEVEL%%") From bdf25a794f0399a1d27214b9a4012119ef5acec9 Mon Sep 17 00:00:00 2001 From: Titus Fortner Date: Mon, 19 Jan 2026 12:13:12 -0600 Subject: [PATCH 2/2] update with feedback --- dotnet/private/nuget_push.bzl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/dotnet/private/nuget_push.bzl b/dotnet/private/nuget_push.bzl index 13152e1773bd9..9bc2510c6997a 100644 --- a/dotnet/private/nuget_push.bzl +++ b/dotnet/private/nuget_push.bzl @@ -23,16 +23,27 @@ def _nuget_push_impl(ctx): ), ] +def _to_runfiles_path(short_path): + """Convert a short_path to a runfiles path. + + External repos in bzlmod have paths like ../repo_name/path, + which in runfiles becomes repo_name/path. + Main repo paths need _main/ prefix. + """ + if short_path.startswith("../"): + return short_path[3:] + return "_main/" + short_path + def _create_unix_script(ctx, dotnet, nupkg_files): """Create bash script for Unix/macOS/Linux.""" push_commands = [] for nupkg in nupkg_files: + nupkg_runfiles_path = _to_runfiles_path(nupkg.short_path) push_commands.append( - '"$DOTNET" nuget push "$RUNFILES_DIR/_main/{nupkg}" --api-key "$NUGET_API_KEY" --source "$NUGET_SOURCE" --skip-duplicate --no-symbols'.format(nupkg = nupkg.short_path), + '"$DOTNET" nuget push "$RUNFILES_DIR/{nupkg}" --api-key "$NUGET_API_KEY" --source "$NUGET_SOURCE" --skip-duplicate'.format(nupkg = nupkg_runfiles_path), ) - # External repos in bzlmod have paths like ../repo_name/path, which in runfiles becomes repo_name/path - dotnet_runfiles_path = dotnet.short_path.lstrip("../") + dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path) script_content = """#!/usr/bin/env bash set -euo pipefail @@ -43,7 +54,8 @@ if [[ -d "$0.runfiles/_main" ]]; then elif [[ -n "${{RUNFILES_DIR:-}}" ]]; then RUNFILES_DIR="$RUNFILES_DIR" else - RUNFILES_DIR="$(cd "$(dirname "$0")" && pwd)" + echo "ERROR: Could not locate runfiles directory" >&2 + exit 1 fi DOTNET="$RUNFILES_DIR/{dotnet}" @@ -65,19 +77,19 @@ def _create_windows_script(ctx, dotnet, nupkg_files): """Create batch script for Windows.""" push_commands = [] for nupkg in nupkg_files: - nupkg_path = nupkg.short_path.replace("/", "\\") + nupkg_runfiles_path = _to_runfiles_path(nupkg.short_path).replace("/", "\\") push_commands.append( - '"%%DOTNET%%" nuget push "%s" --api-key "%%NUGET_API_KEY%%" --source "%%NUGET_SOURCE%%" --skip-duplicate --no-symbols' % nupkg_path, + '"%%DOTNET%%" nuget push "%%~dp0%s" --api-key "%%NUGET_API_KEY%%" --source "%%NUGET_SOURCE%%" --skip-duplicate' % nupkg_runfiles_path, ) push_commands.append("if %%ERRORLEVEL%% neq 0 exit /b %%ERRORLEVEL%%") - dotnet_path = dotnet.short_path.replace("/", "\\") + dotnet_runfiles_path = _to_runfiles_path(dotnet.short_path).replace("/", "\\") script_content = """@echo off set DOTNET=%~dp0{dotnet_path} {push_commands} """.format( - dotnet_path = dotnet_path, + dotnet_path = dotnet_runfiles_path, push_commands = "\n".join(push_commands), )