#How to properly call sub commands?

1 messages Β· Page 1 of 1 (latest)

dawn sorrel
#

I have a very simple typescript dagger setup. Basically the hello world of things. I put a object and function in a different folder following the instuctions at https://docs.dagger.io/developer-guide/typescript/370239/module-structure but am having issues calling my subcommand. Is there a specific syntax in order to call command brought in like this?

It's essential to understand a few key concepts about Dagger Modules created for use with the TypeScript SDK, for a better fit with your normal development workflow.

#

Kind of seems like sub command just dont work

fringe viper
#

cc @silver abyss our resident Python SDK expert

#

The syntax of the dagger call command looks fine. I can't speak to the code of the module. My guess it that the problem is on that side. Are you able to share the code?

dawn sorrel
#

This is in the Typescript SDK not Python.

fringe viper
#

Then copying @latent bough πŸ™‚

#

Maybe @misty fiber or @proven kernel can point to other modules in Typescript, for quick comparison?

proven kernel
#

taking a look

#

In a new dir I did

dagger init --sdk typescript --name motion-ci --source .
dagger call container-echo --string-arg Hi stdout

Which works

#

Notice in the top image where the module name is "MotionCi" and the function being called by Dagger is "containerEcho"

#

which is a default function provided by dagger init as an example.

#

I see in your invocation

#

so Dagger is looking for an "echo" function

#

you could add one to your index.ts file

@func()
  echo(stringArg: string): Container {
    return dag.container().from("alpine:latest").withExec(["echo", stringArg])
  }
#

and then call

#

Does that help? Then you could move on to calling that first module from another one πŸ™‚

dawn sorrel
proven kernel
dawn sorrel
#

Yeah exactly.

proven kernel
#

trying it πŸ™‚

src ➀ cp index.ts echo.ts
src ➀ cp index.ts grep.ts
# editing to hack the one index file's contents into two
#
import { dag, Container, Directory, object, func } from "@dagger.io/dagger"

import { Echo } from "./echo" // in src/echo.ts
import { Grep } from "./grep" // in src/grep.ts
dawn sorrel
#

Does that setup work on your side?

proven kernel
#

nope 😒 I can't get it to work for me.

#

I'm no master of TypeScript, but yeah, I tried it like this.

import { dag, Container, Directory, object, func } from "@dagger.io/dagger"

import { Echo } from "./echo" // in src/echo.ts
import { Grep } from "./grep" // in src/grep.ts

@object()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class MotionCi {}
  @field()
  echo: Echo = new Echo()

  @field()
  grep: Grep = new Grep()
}
#

like docs

#

and also as a wrapper function. Will look a little harder...

I'm using dagger functions to see if any functions are registered on my Class.

dawn sorrel
#

It seems like dagger can see that the inner function command exists. But when I try to call outut in my case, it break just like before.

proven kernel
#

Ah, could you share how your project is structured. Maybe a repo or a gist and a tree?

dawn sorrel
#

I tried with echo in a sub dir as well as at the same level like this, either way, same result.

proven kernel
#

We'll figure it out and follow up. Thanks for the details!

dawn sorrel
#

Wanted to follow up here and see if there were any updates?

silver abyss
#

Hi, I may be able to help.

#

That way of doing new Test() in @field looks suspicious to me. If I recall correctly that uses the implicit constructor that gave some issues in initialization so it was changed to require the constructor to be defined explicitly. Correct me if I'm wrong @latent bough, if so the docs need to be updated. @dawn sorrel, try the @func form:

import { object, func } from "@dagger.io/dagger"

import { Echo } from "./echo" // in src/echo.ts
import { Grep } from "./grep" // in src/grep.ts

@object()
class MotionCi {
  @func()
  echo(): Echo {
      return new Echo()
  }

  @func()
  grep(): Grep {
      return new Grep()
  }
}
dawn sorrel
#

Maybe the docs do need to be updated. But the CLI seems to indicate that the way things are setup in docs is right. See my message above with 2 screenshots. The CLI says there is an output command under the Echo command?

silver abyss
#

Yes, it reports the function but can't execute it. It should either error and not report it, or fix execution.

#

Either way it's a bug.

#

And docs should show the @func form instead.

dawn sorrel
#

I'll try out the @func form here in just a minute

#

Yup. That seemed to do the trick! Would you like me to file bugs for the docs issue as well as the issue around that preivous command reporting an error?

silver abyss
#

Yes, please πŸ˜„

latent bough
#

I'm checking this issue right now, I tested that kind of pattern using the @func() decorator and it worked, but I didn't with @field(), I'll add it to my tests cases

latent bough
#

Ok I did a quick tests with @func() again, I confirm that it works as expected

// index.ts
import { object, func } from "@dagger.io/dagger"
import { Lint } from './lint';
import { Test } from './test'

@object()
class Obj {
  @func()
  test(): Test {
    return new Test()
  }

  @func()
  lint(): Lint {
    return new Lint()
  }
}
// test.ts
import { object, func } from "@dagger.io/dagger"

@object()
export class Test {
    @func()
    echo(): string {
        return "world"
    }
}
import { object, func } from "@dagger.io/dagger"

@object()
export class Lint {
    @func()
    echo(): string {
        return "world"
    }
}
#

If you have fields with defaults initialization in the constructor, you'll need to specify a constructor in order to init these objects (because it cannot be done from a default value)

import { object, field } from "@dagger.io/dagger"
import { Lint } from './lint';
import { Test } from './test'

@object()
class Obj {
  @field()
  test: Test = new Test()

  @field()
  lint: Lint = new Lint()

  // Force call the constructor to init objects
  constructor() {}
}
#

However, I found an issue when I tried to call test or lint

Error: could not find result property lint  

I know where this comes from, I'll publish a fix today

#

@upbeat lotus ^to merge asap please πŸ˜„

upbeat lotus
upbeat lotus