1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

プロジェクト作成時に依存関係を復元(dotnet restore)させる

Last updated at Posted at 2018-09-30

tl;dr

template.jsonpostActionsprimaryOutputsの項目を追加することで、カスタムテンプレートがdotnet restoreを自動実行するよう設定できる。

環境

$ dotnet --version
2.1.402
$ nuget help
NuGet Version: 4.8.1.5435

背景

dotnet-template-samples/16-nuget-packageをそのままnuget pack, dotnet new -i, dotnet new garciaconsoleして使ってみると、VSCodeでは次の内容のポップアップダイアログが立ち上がります。またコードには赤線が引かれ、IntelliSenseも効かない状態なってしまいます。

There are unresolved dependencies'. Please execute the restore command to continue.

ここでRestoreボタンを押すか、dotnet restoreを実行すると適切なファイルが生成され、正常な状態に戻ります。

暗黙的dotnet restoreなるものがあるらしいけど、これがうまく働かない理由は未調査)

Restoreの自動実行

既存のconsoleテンプレートの出力を見ると、確かにdotnet restoreが実行されているようです。そこでdotnet restoreを自動実行させるため、今度はdotnet-template-samples/08-restore-on-createを参考にして以下の内容をtemplate.jsonに追加してみます。

"postActions": [
    {
        "manualInstructions": [
            {
                "text": "Run 'dotnet restore'"
            }
        ],
        "actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
        "continueOnError": true
    }
]

しかし、このテンプレートを使ってもやはりrestoreが上手くいきません。次のようなメッセージが表示されるでしょう。

作成後のアクションを処理しています...
復元するプライマリ出力がありません。

csprojの指定

実はrestoreするためには*projファイルを指定してやる必要があります。そこで次の内容をtemplate.jsonに追加します。

"primaryOutputs": [
    {
       "path": "content.csproj"
    }
]

これによりdotnet restoreが正しく実行されるようになります。

参考リンク

dotnet new のカスタム テンプレート | Microsoft Docs
Post Action Registry · dotnet/templating Wiki
Restore fails with "No Primary Outputs to restore" · Issue #1324 · dotnet/templating

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?