Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

besom with sbt #554

Open
greshny opened this issue Dec 27, 2024 · 4 comments
Open

besom with sbt #554

greshny opened this issue Dec 27, 2024 · 4 comments

Comments

@greshny
Copy link

greshny commented Dec 27, 2024

I was looking into integration tests and came up with the following structure

build.sbt:

ThisBuild / scalaVersion := "3.5.2"

lazy val root = project
  .in(file("."))
  .aggregate(infra)

lazy val infra = project
  .in(file("modules/infra"))
  .settings(
    libraryDependencies ++= Seq(
      "org.virtuslab" %% "besom-command" % "0.10.0-core.0.3"
    ),
    addCompilerPlugin(
      "org.virtuslab" %% "besom-compiler-plugin" % "0.3.2"
    )
  )

I have the following directory structure:

modules/infra
├── Pulumi.yml
└── src/main/scala/infra
    ├── Main.scala

modules/infra/Pulumi.yml

name: test-app
runtime: scala
description: "test besom/pulumi infra app"

modules/infra/src/main/scala/infra/Main.scala

package infra

import besom.*

object Main:
  @main def run = Pulumi.run:
    Stack(
      log.warn("Nothing's here yet, it's waiting for you to write some code!")
    )

Although, I'm not sure if it's proper way. If I try to run, let's say sbt infra/run, it would require env variables

What would be correct way to use besom inside of a sbt project?

@greshny
Copy link
Author

greshny commented Dec 27, 2024

@greshny
Copy link
Author

greshny commented Dec 27, 2024

± |main ✓| → PULUMI_PROJECT=test-app sbt infra/run
[info] welcome to sbt 1.10.1 (Eclipse Adoptium Java 21.0.5)
[info] loading global plugins from /Users/roman/.sbt/1.0/plugins
[info] loading settings for project besom-with-sbt-build-build from metals.sbt ...
[info] loading project definition from /Users/roman/git/home/besom-with-sbt/project/project
[info] loading settings for project besom-with-sbt-build from metals.sbt ...
[info] loading project definition from /Users/roman/git/home/besom-with-sbt/project
[success] Generated .bloop/besom-with-sbt-build.json
[success] Total time: 1 s, completed 27 Dec 2024, 17:10:29
[info] loading settings for project root from build.sbt ...
[info] set current project to root (in build file:/Users/roman/git/home/besom-with-sbt/)
[info] running infra.run 
2024.12.27 17:10:31:077 scala-execution-context-global-176  ERROR besom.internal.RunInfo.fromEnv:38
    Error during initial run configuration resolution: Error: environment variable 'PULUMI_STACK' not present!
2024.12.27 17:10:31:119 sbt-bg-threads-1  ERROR besom.internal.BesomModule.run:70
    java.lang.Exception: Error: environment variable 'PULUMI_STACK' not present!
    	at besom.internal.Env$.getOrFail$$anonfun$2(Env.scala:40)
    	at scala.Option.getOrElse(Option.scala:201)
    	at besom.internal.Env$.getOrFail(Env.scala:41)
    	at besom.internal.Env$.stack$lzyINIT1(Env.scala:67)
    	at besom.internal.Env$.stack(Env.scala:67)
    	at besom.internal.RunInfo$.fromEnv$$anonfun$1$$anonfun$1(RunInfo.scala:27)
    	at scala.util.Try$.apply(Try.scala:217)
    	at besom.internal.RunInfo$.fromEnv$$anonfun$1(RunInfo.scala:35)
    	at besom.internal.Result$.evalTry$$anonfun$1(Result.scala:301)
    	at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:687)
    	at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:467)
    	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1423)
    	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
    	at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
    	at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
    	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
    	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)
    
[error] java.lang.Exception: Error: environment variable 'PULUMI_STACK' not present!
[error] 	at besom.internal.Env$.getOrFail$$anonfun$2(Env.scala:40)
[error] 	at scala.Option.getOrElse(Option.scala:201)

@greshny
Copy link
Author

greshny commented Dec 27, 2024

Seems I have figured out: greshny-forks/besom-with-sbt#1

  1. The Pulumi.yaml file needs to be in the root directory of the project.
  2. I should have used pulumi up -y --stack dev instead of sbt infra/run
  3. also @main annotation didn't work for me, and I received an error:
 pulumi up -y --stack dev
Previewing update (dev)

View Live: https://app.pulumi.com/greshny/test-app/dev/previews/c4f043ae-c1d5-477e-9825-53e77f07c596

@ Previewing update............

@ Previewing update..........
    pulumi:pulumi:Stack test-app-dev  [info] welcome to sbt 1.10.1 (Eclipse Adoptium Java 21.0.5)
    pulumi:pulumi:Stack test-app-dev  [info] loading global plugins from /Users/roman/.sbt/1.0/plugins
    pulumi:pulumi:Stack test-app-dev  [info] loading settings for project besom-with-sbt-build-build from metals.sbt ...
    pulumi:pulumi:Stack test-app-dev  [info] loading project definition from /Users/roman/git/home/besom-with-sbt/project/project
    pulumi:pulumi:Stack test-app-dev  [info] loading settings for project besom-with-sbt-build from metals.sbt ...
    pulumi:pulumi:Stack test-app-dev  [info] loading project definition from /Users/roman/git/home/besom-with-sbt/project
    pulumi:pulumi:Stack test-app-dev  [success] Generated .bloop/besom-with-sbt-build.json
    pulumi:pulumi:Stack test-app-dev  [success] Total time: 1 s, completed 27 Dec 2024, 21:21:31
    pulumi:pulumi:Stack test-app-dev  [info] loading settings for project root from build.sbt ...
    pulumi:pulumi:Stack test-app-dev  [info] set current project to root (in build file:/Users/roman/git/home/besom-with-sbt/)
    pulumi:pulumi:Stack test-app-dev  [info] running infra.Main 
    pulumi:pulumi:Stack test-app-dev  [error] java.lang.NoSuchMethodException: infra.Main.main([Ljava.lang.String;)
    pulumi:pulumi:Stack test-app-dev  [error] 	at java.base/java.lang.Class.getMethod(Class.java:2395)
    pulumi:pulumi:Stack test-app-dev  [error] stack trace is suppressed; run last Compile / run for the full output
    pulumi:pulumi:Stack test-app-dev  [error] (Compile / run) java.lang.NoSuchMethodException: infra.Main.main([Ljava.lang.String;)
    pulumi:pulumi:Stack test-app-dev  [error] Total time: 1 s, completed 27 Dec 2024, 21:21:33
@ Previewing update....
    pulumi:pulumi:Stack test-app-dev  error: an unhandled error occurred: '/usr/local/bin/sbt /usr/local/bin/sbt -batch run' exited with non-zero exit code: 1
    pulumi:pulumi:Stack test-app-dev  
    pulumi:pulumi:Stack test-app-dev  1 error; 17 messages
Diagnostics:
  pulumi:pulumi:Stack (test-app-dev):
    [info] welcome to sbt 1.10.1 (Eclipse Adoptium Java 21.0.5)
    [info] loading global plugins from /Users/roman/.sbt/1.0/plugins
    [info] loading settings for project besom-with-sbt-build-build from metals.sbt ...
    [info] loading project definition from /Users/roman/git/home/besom-with-sbt/project/project
    [info] loading settings for project besom-with-sbt-build from metals.sbt ...
    [info] loading project definition from /Users/roman/git/home/besom-with-sbt/project
    [success] Generated .bloop/besom-with-sbt-build.json
    [success] Total time: 1 s, completed 27 Dec 2024, 21:21:31
    [info] loading settings for project root from build.sbt ...
    [info] set current project to root (in build file:/Users/roman/git/home/besom-with-sbt/)
    [info] running infra.Main 
    [error] java.lang.NoSuchMethodException: infra.Main.main([Ljava.lang.String;)
    [error] 	at java.base/java.lang.Class.getMethod(Class.java:2395)
    [error] stack trace is suppressed; run last Compile / run for the full output
    [error] (Compile / run) java.lang.NoSuchMethodException: infra.Main.main([Ljava.lang.String;)
    [error] Total time: 1 s, completed 27 Dec 2024, 21:21:33
    

    error: an unhandled error occurred: '/usr/local/bin/sbt /usr/local/bin/sbt -batch run' exited with non-zero exit code: 1

I needed to use the main method instead of @main def run = ....

  1. I'm not sure how to specify the mainClass for pulumi up when I have multiple sbt modules, such as an infra sbt module and a backend sbt module, each with a mainClass. e.g. do I have to use the following workaround:
lazy val root = project
  .in(file("."))
  .aggregate(infra, backend)
  .dependsOn(infra)
  .settings(
    Compile / mainClass := Some("infra.Main")
  )

@prolativ
Copy link
Collaborator

prolativ commented Jan 2, 2025

  1. You're right, Pulumi.yml should be in the root of the project, next to build.sbt
  2. Then from the root of the project you should run BESOM_SBT_MODULE=infra pulumi up -y --stack dev
  3. @main works for me without problems, when I run the project with the command from the line above
  4. A main method from backend module (or any other module) shouldn't clash with main from infra. You tell pulumi which sbt module to use by specifying BESOM_SBT_MODULE environment variable. However, setting Compile / mainClass might be needed if you had multiple main methods within a single module. One nonobvious thing to note here is how the expected mainClass name is derived from the main method annotated with @main (which is done by scala 3 compiler): the entire package prefix is preserved and the name of the method is used as the name of the class, while the names of all enclosing objects are simply skipped. So in your example it would be infra.run rather than infra.Main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants