From e725bcbf876d2e25c19b7101d3c7e4f5a8d4d405 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Mon, 4 Oct 2021 12:29:10 -0700 Subject: [PATCH 01/35] Added --qsharp-version option. --- src/QsFmt/App/Program.fs | 55 +++++++++++++++++-------- src/QsFmt/Formatter.Tests/Discoverer.fs | 6 +-- src/QsFmt/Formatter.Tests/Errors.fs | 2 +- src/QsFmt/Formatter.Tests/Tests.fs | 2 +- src/QsFmt/Formatter/Formatter.fs | 5 ++- src/QsFmt/Formatter/Formatter.fsi | 6 +-- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index 1479974073..68ef4c2967 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -15,6 +15,7 @@ type Argument = | [] Inputs of string list | [] Backup | [] Recurse + | [] QSharp_Version of string | [] Update | [] Format @@ -24,8 +25,9 @@ type Argument = | Inputs _ -> "Files or folders to format or \"-\" to read from standard input." | Backup -> "Create backup files of input files." | Recurse -> "Process the input folder recursively." - | Update _ -> "Update depreciated syntax in the input files." - | Format _ -> "Format the source code in input files." + | QSharp_Version _ -> "Provides a Q# version to the tool." + | Update -> "Update depreciated syntax in the input files." + | Format -> "Format the source code in input files." type CommandKind = | Update @@ -36,6 +38,7 @@ type Arguments = CommandKind: CommandKind RecurseFlag: bool BackupFlag: bool + QSharp_Version: Version option Inputs: string list } @@ -77,8 +80,8 @@ let run arguments inputs = let command = match arguments.CommandKind with - | Update -> Formatter.update input - | Format -> Formatter.format + | Update -> Formatter.update input arguments.QSharp_Version + | Format -> Formatter.format arguments.QSharp_Version match command source with | Ok result -> @@ -110,20 +113,36 @@ let main args = try let results = parser.Parse args - let args = - { - CommandKind = - match results.TryGetSubCommand() with - | None // default to update command - | Some Argument.Update -> Update - | Some Argument.Format -> Format - | _ -> failwith "unrecognized command used" - RecurseFlag = results.Contains Recurse - BackupFlag = results.Contains Backup - Inputs = results.GetResult Inputs - } - - args.Inputs |> run args + let mutable validVersion = true + let version = + match results.TryGetResult QSharp_Version with + | Some x -> + match Version.TryParse x with + | true, v -> Some v + | _ -> + validVersion <- false + None + | None -> None + + if validVersion then + let args = + { + CommandKind = + match results.TryGetSubCommand() with + | None // default to update command + | Some Argument.Update -> Update + | Some Argument.Format -> Format + | _ -> failwith "unrecognized command used" + RecurseFlag = results.Contains Recurse + BackupFlag = results.Contains Backup + QSharp_Version = version + Inputs = results.GetResult Inputs + } + + args.Inputs |> run args + else + eprintf "Error: Bad version number." + 2 with | :? ArguParseException as ex -> eprintf "%s" ex.Message diff --git a/src/QsFmt/Formatter.Tests/Discoverer.fs b/src/QsFmt/Formatter.Tests/Discoverer.fs index 2e965b217a..0b4bb52743 100644 --- a/src/QsFmt/Formatter.Tests/Discoverer.fs +++ b/src/QsFmt/Formatter.Tests/Discoverer.fs @@ -183,7 +183,7 @@ module Discoverer = | Some reason -> Skip.If(true, reason) | None -> let after = example.After |> standardizeNewLines |> Ok |> ShowResult - let before = Formatter.format example.Before |> Result.map standardizeNewLines |> ShowResult + let before = Formatter.format None example.Before |> Result.map standardizeNewLines |> ShowResult Assert.Equal(after, before) /// @@ -197,7 +197,7 @@ module Discoverer = | Some reason -> Skip.If(true, reason) | None -> let after = example.After |> standardizeNewLines |> Ok |> ShowResult - let before = Formatter.update "" example.Before |> Result.map standardizeNewLines |> ShowResult + let before = Formatter.update "" None example.Before |> Result.map standardizeNewLines |> ShowResult Assert.Equal(after, before) /// @@ -210,5 +210,5 @@ module Discoverer = | Some reason -> Skip.If(true, reason) | None -> let original = Ok fixedPoint.Source |> ShowResult - let formatted = Formatter.format fixedPoint.Source |> ShowResult + let formatted = Formatter.format None fixedPoint.Source |> ShowResult Assert.Equal(original, formatted) diff --git a/src/QsFmt/Formatter.Tests/Errors.fs b/src/QsFmt/Formatter.Tests/Errors.fs index 8068a4b45b..310ddc75c9 100644 --- a/src/QsFmt/Formatter.Tests/Errors.fs +++ b/src/QsFmt/Formatter.Tests/Errors.fs @@ -9,7 +9,7 @@ open Xunit [] let ``Returns error result with syntax errors`` () = - let result = Formatter.format "namespace Foo { invalid syntax; }" |> Result.mapError (List.map string) + let result = Formatter.format None "namespace Foo { invalid syntax; }" |> Result.mapError (List.map string) let error = "Line 1, Character 16: mismatched input 'invalid' expecting {'function', 'internal', 'newtype', 'open', 'operation', '@', '}'}" diff --git a/src/QsFmt/Formatter.Tests/Tests.fs b/src/QsFmt/Formatter.Tests/Tests.fs index 303a65aa52..feebbf524b 100644 --- a/src/QsFmt/Formatter.Tests/Tests.fs +++ b/src/QsFmt/Formatter.Tests/Tests.fs @@ -22,7 +22,7 @@ let private run input expectedOutput expectedWarnings = try Console.SetError error - let output = Formatter.update "" input |> Result.map standardizeNewLines + let output = Formatter.update "" None input |> Result.map standardizeNewLines let warnings = error.ToString() |> standardizeNewLines Assert.Equal(expectedOutput, output) Assert.Equal(expectedWarnings, warnings) diff --git a/src/QsFmt/Formatter/Formatter.fs b/src/QsFmt/Formatter/Formatter.fs index 13c02caee1..a607643829 100644 --- a/src/QsFmt/Formatter/Formatter.fs +++ b/src/QsFmt/Formatter/Formatter.fs @@ -10,6 +10,7 @@ open Microsoft.Quantum.QsFmt.Formatter.Printer open Microsoft.Quantum.QsFmt.Formatter.Rules open Microsoft.Quantum.QsFmt.Formatter.Utils open Microsoft.Quantum.QsFmt.Parser +open System open System.Collections.Immutable /// @@ -32,7 +33,7 @@ let parse (source: string) = errorListener.SyntaxErrors |> Error [] -let format source = +let format (qsharp_version : Version option) source = let formatDocument document = let unparsed = printer.Document document @@ -57,7 +58,7 @@ let format source = parse source |> Result.map formatDocument [] -let update fileName source = +let update fileName (qsharp_version : Version option) source = let updateDocument document = let updatedDocument = diff --git a/src/QsFmt/Formatter/Formatter.fsi b/src/QsFmt/Formatter/Formatter.fsi index a3ab215ba2..293bf56717 100644 --- a/src/QsFmt/Formatter/Formatter.fsi +++ b/src/QsFmt/Formatter/Formatter.fsi @@ -5,15 +5,15 @@ module Microsoft.Quantum.QsFmt.Formatter.Formatter open Microsoft.Quantum.QsFmt.Formatter.Errors +open System /// Formats the given Q# source code. [] -val format: string -> Result +val format: Version option -> string -> Result /// Updates deprecated syntax in the given source code. -/// Takes a Q# file name and source code. [] -val update: string -> string -> Result +val update: string -> Version option -> string -> Result /// Parses then un-parses the given Q# source code without formatting. [] From 1d2b65e1351551cbdc164374e26c1b8b4a89eb92 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Mon, 4 Oct 2021 14:11:12 -0700 Subject: [PATCH 02/35] Changed the checker to be specific to array syntax. --- src/QsFmt/Formatter/Formatter.fs | 57 +++++++++++++++++++++++++------- src/QsFmt/Formatter/Rules.fs | 2 +- src/QsFmt/Formatter/Rules.fsi | 5 ++- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/QsFmt/Formatter/Formatter.fs b/src/QsFmt/Formatter/Formatter.fs index a607643829..859936f359 100644 --- a/src/QsFmt/Formatter/Formatter.fs +++ b/src/QsFmt/Formatter/Formatter.fs @@ -12,6 +12,7 @@ open Microsoft.Quantum.QsFmt.Formatter.Utils open Microsoft.Quantum.QsFmt.Parser open System open System.Collections.Immutable +open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree /// /// Parses the Q# source code into a . @@ -32,6 +33,24 @@ let parse (source: string) = else errorListener.SyntaxErrors |> Error +let versionToFormatRules (version : Version option) = + let simpleRule (rule : unit Rewriter) = + curry rule.Document () + let rules = + match version with + // The following lines are provided as examples of different rules for different versions: + //| Some v when v < new Version("1.0") -> [simpleRule collapsedSpaces] + //| Some v when v < new Version("1.5") -> [simpleRule operatorSpacing] + | None + | Some _ -> + [ + simpleRule collapsedSpaces + simpleRule operatorSpacing + simpleRule newLines + curry indentation.Document 0 + ] + rules |> List.fold (>>) id + [] let format (qsharp_version : Version option) source = let formatDocument document = @@ -41,10 +60,7 @@ let format (qsharp_version : Version option) source = if unparsed = source then // The actual format process document - |> curry collapsedSpaces.Document () - |> curry operatorSpacing.Document () - |> curry newLines.Document () - |> curry indentation.Document 0 + |> versionToFormatRules qsharp_version |> printer.Document // Report error if the unparsing result does not match the original source else @@ -57,18 +73,35 @@ let format (qsharp_version : Version option) source = parse source |> Result.map formatDocument +let versionToUpdateRules (version : Version option) = + let simpleRule (rule : unit Rewriter) = + curry rule.Document () + let rules = + match version with + // The following lines are provided as examples of different rules for different versions: + //| Some v when v < new Version("1.0") -> [simpleRule forParensUpdate] + //| Some v when v < new Version("1.5") -> [simpleRule unitUpdate] + | None + | Some _ -> + [ + simpleRule qubitBindingUpdate + simpleRule unitUpdate + simpleRule forParensUpdate + simpleRule arraySyntaxUpdate + ] + rules |> List.fold (>>) id + [] let update fileName (qsharp_version : Version option) source = let updateDocument document = - let updatedDocument = - document - |> curry qubitBindingUpdate.Document () - |> curry unitUpdate.Document () - |> curry forParensUpdate.Document () - |> curry arraySyntaxUpdate.Document () - - let warningList = updatedDocument |> updateChecker fileName + let updatedDocument = versionToUpdateRules qsharp_version document + let warningList = + match qsharp_version with + // The following line is provided as an example of different rules for different versions: + //| Some v when v < new Version("1.5") -> [] + | None + | Some _ -> updatedDocument |> checkArraySyntax fileName let printedDocument = updatedDocument |> printer.Document warningList |> List.iter (eprintfn "%s") diff --git a/src/QsFmt/Formatter/Rules.fs b/src/QsFmt/Formatter/Rules.fs index f22c6f26ce..2b87ea8f2b 100644 --- a/src/QsFmt/Formatter/Rules.fs +++ b/src/QsFmt/Formatter/Rules.fs @@ -261,7 +261,7 @@ let arraySyntaxUpdate = | _ -> base.Expression((), expression) } -let updateChecker fileName document = +let checkArraySyntax fileName document = let mutable lineNumber = 1 let mutable charNumber = 1 diff --git a/src/QsFmt/Formatter/Rules.fsi b/src/QsFmt/Formatter/Rules.fsi index 99d321a654..c5d3915ab7 100644 --- a/src/QsFmt/Formatter/Rules.fsi +++ b/src/QsFmt/Formatter/Rules.fsi @@ -32,6 +32,5 @@ val forParensUpdate: unit Rewriter /// Updates the `new [n]` array syntax to the new `[val, size = n]` array syntax. val arraySyntaxUpdate: unit Rewriter -/// Provides warnings for deprecated syntax still in the syntax tree. -/// Takes the file name as well as the syntax tree. -val updateChecker: string -> Document -> string list +/// Provides warnings for deprecated array syntax still in the syntax tree. +val checkArraySyntax: string -> Document -> string list From 23e3b41cdaac0dd1aeb44042da3eb177a2ef0541 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Mon, 4 Oct 2021 16:40:26 -0700 Subject: [PATCH 03/35] small change --- src/QsFmt/Formatter/Formatter.fs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/QsFmt/Formatter/Formatter.fs b/src/QsFmt/Formatter/Formatter.fs index 859936f359..9cb407edfb 100644 --- a/src/QsFmt/Formatter/Formatter.fs +++ b/src/QsFmt/Formatter/Formatter.fs @@ -33,9 +33,10 @@ let parse (source: string) = else errorListener.SyntaxErrors |> Error +let simpleRule (rule : unit Rewriter) = + curry rule.Document () + let versionToFormatRules (version : Version option) = - let simpleRule (rule : unit Rewriter) = - curry rule.Document () let rules = match version with // The following lines are provided as examples of different rules for different versions: @@ -74,8 +75,6 @@ let format (qsharp_version : Version option) source = parse source |> Result.map formatDocument let versionToUpdateRules (version : Version option) = - let simpleRule (rule : unit Rewriter) = - curry rule.Document () let rules = match version with // The following lines are provided as examples of different rules for different versions: From d782dbd51a3b41e586a8dbeedd032e623a1e6f43 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 5 Oct 2021 10:17:24 -0700 Subject: [PATCH 04/35] style --- src/QsFmt/Formatter/Formatter.fs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/QsFmt/Formatter/Formatter.fs b/src/QsFmt/Formatter/Formatter.fs index 9cb407edfb..976126c636 100644 --- a/src/QsFmt/Formatter/Formatter.fs +++ b/src/QsFmt/Formatter/Formatter.fs @@ -33,10 +33,9 @@ let parse (source: string) = else errorListener.SyntaxErrors |> Error -let simpleRule (rule : unit Rewriter) = - curry rule.Document () +let simpleRule (rule: unit Rewriter) = curry rule.Document () -let versionToFormatRules (version : Version option) = +let versionToFormatRules (version: Version option) = let rules = match version with // The following lines are provided as examples of different rules for different versions: @@ -50,19 +49,18 @@ let versionToFormatRules (version : Version option) = simpleRule newLines curry indentation.Document 0 ] + rules |> List.fold (>>) id [] -let format (qsharp_version : Version option) source = +let format (qsharp_version: Version option) source = let formatDocument document = let unparsed = printer.Document document // Test whether there is data loss during parsing and unparsing if unparsed = source then // The actual format process - document - |> versionToFormatRules qsharp_version - |> printer.Document + document |> versionToFormatRules qsharp_version |> printer.Document // Report error if the unparsing result does not match the original source else failwith ( @@ -74,7 +72,7 @@ let format (qsharp_version : Version option) source = parse source |> Result.map formatDocument -let versionToUpdateRules (version : Version option) = +let versionToUpdateRules (version: Version option) = let rules = match version with // The following lines are provided as examples of different rules for different versions: @@ -88,19 +86,22 @@ let versionToUpdateRules (version : Version option) = simpleRule forParensUpdate simpleRule arraySyntaxUpdate ] + rules |> List.fold (>>) id [] -let update fileName (qsharp_version : Version option) source = +let update fileName (qsharp_version: Version option) source = let updateDocument document = let updatedDocument = versionToUpdateRules qsharp_version document + let warningList = match qsharp_version with // The following line is provided as an example of different rules for different versions: //| Some v when v < new Version("1.5") -> [] | None | Some _ -> updatedDocument |> checkArraySyntax fileName + let printedDocument = updatedDocument |> printer.Document warningList |> List.iter (eprintfn "%s") From 727d997b932945484423e0e69d0167833c441fb8 Mon Sep 17 00:00:00 2001 From: Bettina Heim Date: Tue, 5 Oct 2021 15:57:44 -0700 Subject: [PATCH 05/35] draft for getting the Q# files in a project given the project file path --- QsFmt.sln | 9 ++---- src/QsFmt/App/App.fsproj | 5 +++ src/QsFmt/App/DesignTimeBuild.fs | 53 ++++++++++++++++++++++++++++++++ src/QsFmt/App/Program.fs | 28 +++++++++++++++-- 4 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 src/QsFmt/App/DesignTimeBuild.fs diff --git a/QsFmt.sln b/QsFmt.sln index 345a97b8f8..12e4a7d425 100644 --- a/QsFmt.sln +++ b/QsFmt.sln @@ -9,14 +9,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Formatter", "src\QsFmt\Form EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "App", "src\QsFmt\App\App.fsproj", "{68F54AEE-C852-4FFA-831D-5BA741BE893E}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4399BDF1-685A-4373-8748-142C835A52D6}" - ProjectSection(SolutionItems) = preProject - README.md = src\QsFmt\README.md - EndProjectSection +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Formatter.Tests", "src\QsFmt\Formatter.Tests\Formatter.Tests.fsproj", "{FC9E2BEC-F15F-4818-B0E1-805EA554269E}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Formatter.Tests", "src\QsFmt\Formatter.Tests\Formatter.Tests.fsproj", "{FC9E2BEC-F15F-4818-B0E1-805EA554269E}" -EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "App.Tests", "src\QsFmt\App.Tests\App.Tests.fsproj", "{E00FA726-F852-4D14-90F8-1B0E3E08D822}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "App.Tests", "src\QsFmt\App.Tests\App.Tests.fsproj", "{E00FA726-F852-4D14-90F8-1B0E3E08D822}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/QsFmt/App/App.fsproj b/src/QsFmt/App/App.fsproj index cafe963286..ca43c5c65d 100644 --- a/src/QsFmt/App/App.fsproj +++ b/src/QsFmt/App/App.fsproj @@ -7,6 +7,7 @@ + @@ -17,5 +18,9 @@ + + + + diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs new file mode 100644 index 0000000000..8c2d9e2943 --- /dev/null +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +module Microsoft.Quantum.QsFmt.App.DesignTimeBuild + +open Microsoft.Build.Utilities +open Microsoft.Build.Evaluation +open System.Collections.Generic +open Microsoft.Build.Execution +open System.IO +open System + +let globalProperties = + let dict = new Dictionary<_, _>() + dict.["BuildProjectReferences"] <- "false" + dict.["EnableFrameworkPathOverride"] <- "false" // otherwise msbuild fails on .net 461 projects + dict + +/// +/// Extracts the EvaluatedInclude for all items of the given type in the given project instance, +/// and returns the combined path of the project directory and the evaluated include. +/// +let getItemsByType (project : ProjectInstance) (itemType : string) = + project.Items + |> Seq.where (fun item -> item.ItemType.Equals(itemType, StringComparison.OrdinalIgnoreCase) && item.EvaluatedInclude <> null) + |> Seq.map (fun item -> Path.Combine(project.Directory, item.EvaluatedInclude)) + +let getSourceFiles (projectFile : string) = + + // todo: check project file exists? + + let mutable project = null + try + // Unloading the project unloads the project but *doesn't* clear the cache to be resilient to inconsistent states. + // Hence we actually need to unload all projects, which does make sure the cache is cleared and changes on disk are reflected. + // See e.g. https://github.com/Microsoft/msbuild/issues/795 + if ProjectCollection.GlobalProjectCollection = null then + ProjectCollection.GlobalProjectCollection.UnloadAllProjects(); // needed due to the caching behavior of MS build + let properties = globalProperties // TODO: we may need to revise that + project <- new Project(projectFile, properties, ToolLocationHelper.CurrentToolsVersion); + let instance = project.CreateProjectInstance() + + // do we want to check if the project is a Q# project and if not fail? + //let isQsProject = instance.Targets.ContainsKey "QSharpCompile" + + // get all the source files in the project + let sourceFiles = getItemsByType instance "QSharpCompile" + sourceFiles + // TODO: we also need to find out the version number of the Sdk + + finally + if project <> null && ProjectCollection.GlobalProjectCollection <> null then + ProjectCollection.GlobalProjectCollection.UnloadProject project diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index 1479974073..e82fa79342 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -7,6 +7,8 @@ open Argu open Microsoft.Quantum.QsFmt.Formatter open System open System.IO +open Microsoft.Build.Locator +open System.Runtime.Loader /// A command-line argument. [] @@ -74,12 +76,12 @@ let run arguments inputs = else if arguments.BackupFlag then File.Copy(input, (input + "~"), true) File.ReadAllText input - + let command = match arguments.CommandKind with | Update -> Formatter.update input | Format -> Formatter.format - + match command source with | Ok result -> if input = "-" then printf "%s" result else File.WriteAllText(input, result) @@ -105,6 +107,28 @@ let run arguments inputs = [] [] let main args = + + // We need to set the current directory to the same directory of + // the LanguageServer executable so that it will pick the global.json file + // and force the MSBuildLocator to use .NET Core SDK 3.1 + Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); + // In the case where we actually instantiate a server, we need to "configure" the design time build. + // This needs to be done before any MsBuild packages are loaded. + try + let vsi = MSBuildLocator.RegisterDefaults(); + + // We're using the installed version of the binaries to avoid a dependency between + // the .NET Core SDK version and NuGet. This is a workaround due to the issue below: + // https://github.com/microsoft/MSBuildLocator/issues/86 + AssemblyLoadContext.Default.add_Resolving(new Func<_, _, _>(fun assemblyLoadContext assemblyName -> + let path = Path.Combine(vsi.MSBuildPath, sprintf "%s.dll" assemblyName.Name); + if File.Exists(path) then + assemblyLoadContext.LoadFromAssemblyPath path; + else null)) + with | _ -> + // TODO: give some meaningful warning? + () + let parser = ArgumentParser.Create() try From f8158d1a87a88c1f9b69f7348385b45be5e17053 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 6 Oct 2021 13:44:29 -0700 Subject: [PATCH 06/35] WIP: Fixed issue with relative paths to inputs, got version from project files. --- src/QsFmt/App/DesignTimeBuild.fs | 31 +++++++++++++++-------- src/QsFmt/App/Program.fs | 43 +++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index 8c2d9e2943..aa479711e2 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -20,34 +20,43 @@ let globalProperties = /// Extracts the EvaluatedInclude for all items of the given type in the given project instance, /// and returns the combined path of the project directory and the evaluated include. /// -let getItemsByType (project : ProjectInstance) (itemType : string) = +let getItemsByType (project: ProjectInstance) (itemType: string) = project.Items - |> Seq.where (fun item -> item.ItemType.Equals(itemType, StringComparison.OrdinalIgnoreCase) && item.EvaluatedInclude <> null) - |> Seq.map (fun item -> Path.Combine(project.Directory, item.EvaluatedInclude)) + |> Seq.where + (fun item -> + item.ItemType.Equals(itemType, StringComparison.OrdinalIgnoreCase) && item.EvaluatedInclude <> null) + |> Seq.map (fun item -> Path.Combine(project.Directory, item.EvaluatedInclude)) -let getSourceFiles (projectFile : string) = +let getSourceFiles (projectFile: string) = // todo: check project file exists? let mutable project = null + try // Unloading the project unloads the project but *doesn't* clear the cache to be resilient to inconsistent states. // Hence we actually need to unload all projects, which does make sure the cache is cleared and changes on disk are reflected. // See e.g. https://github.com/Microsoft/msbuild/issues/795 - if ProjectCollection.GlobalProjectCollection = null then - ProjectCollection.GlobalProjectCollection.UnloadAllProjects(); // needed due to the caching behavior of MS build + if ProjectCollection.GlobalProjectCollection = null then + ProjectCollection.GlobalProjectCollection.UnloadAllProjects() // needed due to the caching behavior of MS build + let properties = globalProperties // TODO: we may need to revise that - project <- new Project(projectFile, properties, ToolLocationHelper.CurrentToolsVersion); + project <- new Project(projectFile, properties, ToolLocationHelper.CurrentToolsVersion) let instance = project.CreateProjectInstance() // do we want to check if the project is a Q# project and if not fail? //let isQsProject = instance.Targets.ContainsKey "QSharpCompile" // get all the source files in the project - let sourceFiles = getItemsByType instance "QSharpCompile" - sourceFiles - // TODO: we also need to find out the version number of the Sdk + let sourceFiles = getItemsByType instance "QSharpCompile" |> Seq.toList + + let version = + instance.Properties + |> Seq.tryFind (fun x -> x.Name = "QuantumSdkVersion") + |> Option.map (fun x -> x.EvaluatedValue) + + (sourceFiles, version) finally - if project <> null && ProjectCollection.GlobalProjectCollection <> null then + if project <> null && ProjectCollection.GlobalProjectCollection <> null then ProjectCollection.GlobalProjectCollection.UnloadProject project diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index e82fa79342..ee9056de89 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -9,6 +9,7 @@ open System open System.IO open Microsoft.Build.Locator open System.Runtime.Loader +open Microsoft.Quantum.QsFmt.App.DesignTimeBuild /// A command-line argument. [] @@ -17,6 +18,7 @@ type Argument = | [] Inputs of string list | [] Backup | [] Recurse + | [] Project of string | [] Update | [] Format @@ -26,6 +28,7 @@ type Argument = | Inputs _ -> "Files or folders to format or \"-\" to read from standard input." | Backup -> "Create backup files of input files." | Recurse -> "Process the input folder recursively." + | Project _ -> "The project file for the project to process." | Update _ -> "Update depreciated syntax in the input files." | Format _ -> "Format the source code in input files." @@ -76,12 +79,12 @@ let run arguments inputs = else if arguments.BackupFlag then File.Copy(input, (input + "~"), true) File.ReadAllText input - + let command = match arguments.CommandKind with | Update -> Formatter.update input | Format -> Formatter.format - + match command source with | Ok result -> if input = "-" then printf "%s" result else File.WriteAllText(input, result) @@ -111,21 +114,26 @@ let main args = // We need to set the current directory to the same directory of // the LanguageServer executable so that it will pick the global.json file // and force the MSBuildLocator to use .NET Core SDK 3.1 - Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); + let cwd = Directory.GetCurrentDirectory() + Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory) // In the case where we actually instantiate a server, we need to "configure" the design time build. // This needs to be done before any MsBuild packages are loaded. try - let vsi = MSBuildLocator.RegisterDefaults(); - - // We're using the installed version of the binaries to avoid a dependency between - // the .NET Core SDK version and NuGet. This is a workaround due to the issue below: - // https://github.com/microsoft/MSBuildLocator/issues/86 - AssemblyLoadContext.Default.add_Resolving(new Func<_, _, _>(fun assemblyLoadContext assemblyName -> - let path = Path.Combine(vsi.MSBuildPath, sprintf "%s.dll" assemblyName.Name); - if File.Exists(path) then - assemblyLoadContext.LoadFromAssemblyPath path; - else null)) - with | _ -> + try + let vsi = MSBuildLocator.RegisterDefaults() + + // We're using the installed version of the binaries to avoid a dependency between + // the .NET Core SDK version and NuGet. This is a workaround due to the issue below: + // https://github.com/microsoft/MSBuildLocator/issues/86 + AssemblyLoadContext.Default.add_Resolving ( + new Func<_, _, _>(fun assemblyLoadContext assemblyName -> + let path = Path.Combine(vsi.MSBuildPath, sprintf "%s.dll" assemblyName.Name) + if File.Exists(path) then assemblyLoadContext.LoadFromAssemblyPath path else null) + ) + finally + Directory.SetCurrentDirectory(cwd) + with + | _ -> // TODO: give some meaningful warning? () @@ -134,6 +142,11 @@ let main args = try let results = parser.Parse args + let inputs, qsharp_version = + match results.TryGetResult Project with + | Some p -> getSourceFiles p + | None -> results.GetResult Inputs, None + let args = { CommandKind = @@ -144,7 +157,7 @@ let main args = | _ -> failwith "unrecognized command used" RecurseFlag = results.Contains Recurse BackupFlag = results.Contains Backup - Inputs = results.GetResult Inputs + Inputs = inputs } args.Inputs |> run args From 4e5a72dce1751a121b3340bd56de2c9c6ed2dd0e Mon Sep 17 00:00:00 2001 From: Bettina Heim Date: Thu, 7 Oct 2021 17:23:20 -0700 Subject: [PATCH 07/35] updating command line parsing --- src/QsFmt/App/App.fsproj | 3 +- src/QsFmt/App/Program.fs | 202 ++++++++++++++++++++------------------ src/QsFmt/App/Program.fsi | 9 -- 3 files changed, 106 insertions(+), 108 deletions(-) delete mode 100644 src/QsFmt/App/Program.fsi diff --git a/src/QsFmt/App/App.fsproj b/src/QsFmt/App/App.fsproj index ca43c5c65d..896968277f 100644 --- a/src/QsFmt/App/App.fsproj +++ b/src/QsFmt/App/App.fsproj @@ -8,7 +8,6 @@ - @@ -17,7 +16,7 @@ - + diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index b5443b6796..e66f18611f 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -3,58 +3,74 @@ module Microsoft.Quantum.QsFmt.App.Program -open Argu -open Microsoft.Quantum.QsFmt.Formatter open System +open CommandLine +open CommandLine.Text open System.IO open Microsoft.Build.Locator open System.Runtime.Loader -open Microsoft.Quantum.QsFmt.App.DesignTimeBuild - -/// A command-line argument. -[] -type Argument = - /// The path to the input file. - | [] Inputs of string list - | [] Backup - | [] Recurse - | [] QSharp_Version of string - | [] Project of string - | [] Update - | [] Format - - interface IArgParserTemplate with - member arg.Usage = - match arg with - | Inputs _ -> "Files or folders to format or \"-\" to read from standard input." - | Backup -> "Create backup files of input files." - | Recurse -> "Process the input folder recursively." - | QSharp_Version _ -> "Provides a Q# version to the tool." - | Project _ -> "The project file for the project to process." - | Update -> "Update depreciated syntax in the input files." - | Format -> "Format the source code in input files." - -type CommandKind = - | Update - | Format - -type Arguments = - { - CommandKind: CommandKind - RecurseFlag: bool - BackupFlag: bool - QSharp_Version: Version option - Inputs: string list - } + +//open Microsoft.Quantum.QsFmt.Formatter + + +[ - + diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index e66f18611f..b5443b6796 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -3,74 +3,58 @@ module Microsoft.Quantum.QsFmt.App.Program +open Argu +open Microsoft.Quantum.QsFmt.Formatter open System -open CommandLine -open CommandLine.Text open System.IO open Microsoft.Build.Locator open System.Runtime.Loader - -//open Microsoft.Quantum.QsFmt.Formatter - - -[ module Microsoft.Quantum.QsFmt.Formatter.Tests.FixedPoints +open System.IO open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter open Microsoft.Quantum.QsFmt.Formatter.Tests open Microsoft.Quantum.QsFmt.Parser -open System -open System.IO open Xunit /// diff --git a/src/QsFmt/Formatter/Formatter.fs b/src/QsFmt/Formatter/Formatter.fs index 883003a707..2c93a95e08 100644 --- a/src/QsFmt/Formatter/Formatter.fs +++ b/src/QsFmt/Formatter/Formatter.fs @@ -3,16 +3,16 @@ module Microsoft.Quantum.QsFmt.Formatter.Formatter +open System +open System.Collections.Immutable open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter.Errors open Microsoft.Quantum.QsFmt.Formatter.ParseTree.Namespace open Microsoft.Quantum.QsFmt.Formatter.Printer open Microsoft.Quantum.QsFmt.Formatter.Rules +open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Formatter.Utils open Microsoft.Quantum.QsFmt.Parser -open System -open System.Collections.Immutable -open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree /// /// Parses the Q# source code into a . diff --git a/src/QsFmt/Formatter/Formatter.fsi b/src/QsFmt/Formatter/Formatter.fsi index 293bf56717..0743d83766 100644 --- a/src/QsFmt/Formatter/Formatter.fsi +++ b/src/QsFmt/Formatter/Formatter.fsi @@ -4,8 +4,8 @@ /// The Q# formatter. module Microsoft.Quantum.QsFmt.Formatter.Formatter -open Microsoft.Quantum.QsFmt.Formatter.Errors open System +open Microsoft.Quantum.QsFmt.Formatter.Errors /// Formats the given Q# source code. [] diff --git a/src/QsFmt/Formatter/ParseTree/Expression.fsi b/src/QsFmt/Formatter/ParseTree/Expression.fsi index 9edadeeb3b..48a6262a86 100644 --- a/src/QsFmt/Formatter/ParseTree/Expression.fsi +++ b/src/QsFmt/Formatter/ParseTree/Expression.fsi @@ -3,10 +3,10 @@ namespace Microsoft.Quantum.QsFmt.Formatter.ParseTree +open System.Collections.Immutable open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Parser -open System.Collections.Immutable /// /// Creates syntax tree nodes from a parse tree. diff --git a/src/QsFmt/Formatter/ParseTree/Namespace.fsi b/src/QsFmt/Formatter/ParseTree/Namespace.fsi index 23691502b7..545c1f6470 100644 --- a/src/QsFmt/Formatter/ParseTree/Namespace.fsi +++ b/src/QsFmt/Formatter/ParseTree/Namespace.fsi @@ -3,10 +3,10 @@ namespace Microsoft.Quantum.QsFmt.Formatter.ParseTree +open System.Collections.Immutable open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Parser -open System.Collections.Immutable /// /// Constructors for syntax tree and nodes. diff --git a/src/QsFmt/Formatter/ParseTree/Node.fs b/src/QsFmt/Formatter/ParseTree/Node.fs index 2e75148e92..fa6fef1337 100644 --- a/src/QsFmt/Formatter/ParseTree/Node.fs +++ b/src/QsFmt/Formatter/ParseTree/Node.fs @@ -3,12 +3,12 @@ module Microsoft.Quantum.QsFmt.Formatter.ParseTree.Node +open System.Collections.Generic +open System.Collections.Immutable open Antlr4.Runtime open Antlr4.Runtime.Tree open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Parser -open System.Collections.Generic -open System.Collections.Immutable /// /// The contiguous sequence of tokens in the hidden channel that occur before the token with the given diff --git a/src/QsFmt/Formatter/ParseTree/Node.fsi b/src/QsFmt/Formatter/ParseTree/Node.fsi index 8fdaa8f57f..69b6ecdbd8 100644 --- a/src/QsFmt/Formatter/ParseTree/Node.fsi +++ b/src/QsFmt/Formatter/ParseTree/Node.fsi @@ -4,10 +4,10 @@ /// Tools for creating syntax tree nodes from parse tree nodes. module internal Microsoft.Quantum.QsFmt.Formatter.ParseTree.Node +open System.Collections.Immutable open Antlr4.Runtime open Antlr4.Runtime.Tree open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree -open System.Collections.Immutable /// /// The tokens that occur before the token with the given in diff --git a/src/QsFmt/Formatter/ParseTree/Statement.fsi b/src/QsFmt/Formatter/ParseTree/Statement.fsi index 73e2cf87d3..66dbbb5ebf 100644 --- a/src/QsFmt/Formatter/ParseTree/Statement.fsi +++ b/src/QsFmt/Formatter/ParseTree/Statement.fsi @@ -3,10 +3,10 @@ namespace Microsoft.Quantum.QsFmt.Formatter.ParseTree +open System.Collections.Immutable open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Parser -open System.Collections.Immutable /// /// Creates syntax tree nodes from a parse tree. diff --git a/src/QsFmt/Formatter/ParseTree/Type.fsi b/src/QsFmt/Formatter/ParseTree/Type.fsi index 117f7c6657..574ac0688f 100644 --- a/src/QsFmt/Formatter/ParseTree/Type.fsi +++ b/src/QsFmt/Formatter/ParseTree/Type.fsi @@ -3,10 +3,10 @@ namespace Microsoft.Quantum.QsFmt.Formatter.ParseTree +open System.Collections.Immutable open Antlr4.Runtime open Microsoft.Quantum.QsFmt.Formatter.SyntaxTree open Microsoft.Quantum.QsFmt.Parser -open System.Collections.Immutable /// /// Creates syntax tree nodes from a parse tree. diff --git a/src/QsFmt/Formatter/SyntaxTree/Node.fs b/src/QsFmt/Formatter/SyntaxTree/Node.fs index 7ddc1fb431..2fbbfaff2e 100644 --- a/src/QsFmt/Formatter/SyntaxTree/Node.fs +++ b/src/QsFmt/Formatter/SyntaxTree/Node.fs @@ -3,8 +3,8 @@ namespace Microsoft.Quantum.QsFmt.Formatter.SyntaxTree -open System.Text.RegularExpressions open System +open System.Text.RegularExpressions type Trivia = /// A contiguous region of whitespace. From b1467e12c31c80840e795ed935e85d23dd829661 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 12:33:01 -0700 Subject: [PATCH 20/35] Added fsi files back into the App project to ensure restrictive access levels. --- src/QsFmt/App/App.fsproj | 3 + src/QsFmt/App/CommandLineArguments.fsi | 88 ++++++++++++++++++++++++++ src/QsFmt/App/DesignTimeBuild.fsi | 8 +++ src/QsFmt/App/Program.fsi | 9 +++ 4 files changed, 108 insertions(+) create mode 100644 src/QsFmt/App/CommandLineArguments.fsi create mode 100644 src/QsFmt/App/DesignTimeBuild.fsi create mode 100644 src/QsFmt/App/Program.fsi diff --git a/src/QsFmt/App/App.fsproj b/src/QsFmt/App/App.fsproj index 0af0bf8b6d..bd9b231318 100644 --- a/src/QsFmt/App/App.fsproj +++ b/src/QsFmt/App/App.fsproj @@ -7,8 +7,11 @@ + + + diff --git a/src/QsFmt/App/CommandLineArguments.fsi b/src/QsFmt/App/CommandLineArguments.fsi new file mode 100644 index 0000000000..0c0b059891 --- /dev/null +++ b/src/QsFmt/App/CommandLineArguments.fsi @@ -0,0 +1,88 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +module Microsoft.Quantum.QsFmt.App.Arguments + + open System + open CommandLine + open CommandLine.Text + + /// Object for capturing the arguments used with the `format` command. + [] + type FormatArguments = + { + /// Flag to indicate if the `--backup` option was specified. + Backup: bool + + /// Flag to indicate if the `--recurse` option was specified. + Recurse: bool + + /// Optional argument for the `--qsharp-version` option. + QdkVersion: string + + /// The input files specified by the `--input` argument. + InputFiles: seq + + /// The project file specified by the `--project` argument. + ProjectFile: string + } + with + /// Provides example usage. + static member examples : seq + + /// Object for capturing the arguments used with the `update` command. + [] + type UpdateArguments = + { + /// Flag to indicate if the `--backup` option was specified. + Backup: bool + + /// Flag to indicate if the `--recurse` option was specified. + Recurse: bool + + /// Optional argument for the `--qsharp-version` option. + QdkVersion: string + + /// The input files specified by the `--input` argument. + InputFiles: seq + + /// The project file specified by the `--project` argument. + ProjectFile: string + } + with + /// Provides example usage. + static member examples : seq + + /// The kind of command used + type internal CommandKind = + + /// Represents usage of the `update` command + | Update + + /// Represents usage of the `format` command + | Format + + + /// Represents the fully parsed arguments to the tool. + type internal Arguments = + { + /// Indicates the command specified. + CommandKind: CommandKind + + /// Flag to indicate if the `--recurse` option was specified. + RecurseFlag: bool + + /// Flag to indicate if the `--backup` option was specified. + BackupFlag: bool + + /// Optional Q# version specified. + QSharp_Version: Version option + + /// The paths to the files to process. + Inputs: string list + } + + module internal Arguments = + + /// Creates an Arguments object from an UpdateArguments object + val fromUpdateArguments: UpdateArguments -> Result diff --git a/src/QsFmt/App/DesignTimeBuild.fsi b/src/QsFmt/App/DesignTimeBuild.fsi new file mode 100644 index 0000000000..2f003afc59 --- /dev/null +++ b/src/QsFmt/App/DesignTimeBuild.fsi @@ -0,0 +1,8 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +module internal Microsoft.Quantum.QsFmt.App.DesignTimeBuild + + /// Given a path to a project file, returns the list of files associated + /// to the project and the Quantum SDK version if found. + val getSourceFiles : string -> string list * string option diff --git a/src/QsFmt/App/Program.fsi b/src/QsFmt/App/Program.fsi new file mode 100644 index 0000000000..fa48486b9c --- /dev/null +++ b/src/QsFmt/App/Program.fsi @@ -0,0 +1,9 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/// The command-line interface for the Q# formatter. +module Microsoft.Quantum.QsFmt.App.Program + +/// Runs the Q# formatter. +[] +val main: string [] -> int From 2ac71324640ada0109f5509cb2b42ad09e846d75 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 13:03:30 -0700 Subject: [PATCH 21/35] Added 'Shows syntax errors' test. --- src/QsFmt/App.Tests/App.Tests.fsproj | 3 +++ src/QsFmt/App.Tests/Examples/SyntaxError.qs | 1 + src/QsFmt/App.Tests/Tests.fs | 12 ++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 src/QsFmt/App.Tests/Examples/SyntaxError.qs diff --git a/src/QsFmt/App.Tests/App.Tests.fsproj b/src/QsFmt/App.Tests/App.Tests.fsproj index f7e567a2e3..7920638d83 100644 --- a/src/QsFmt/App.Tests/App.Tests.fsproj +++ b/src/QsFmt/App.Tests/App.Tests.fsproj @@ -6,6 +6,9 @@ + + PreserveNewest + Always diff --git a/src/QsFmt/App.Tests/Examples/SyntaxError.qs b/src/QsFmt/App.Tests/Examples/SyntaxError.qs new file mode 100644 index 0000000000..d2d37ea651 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/SyntaxError.qs @@ -0,0 +1 @@ +namespace Foo { invalid syntax; } diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index ca16982950..66cb73bf60 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -152,6 +152,18 @@ let ``Updates standard input`` () = run [| "update"; "-" |] StandardInputTest.Original ) +[] +let ``Shows syntax errors`` () = + Assert.Equal( + { + Code = 1 + Out = "" + Error = standardizeNewLines "Examples\SyntaxError.qs, Line 1, Character 16: mismatched input 'invalid' expecting {'function', 'internal', 'newtype', 'open', 'operation', '@', '}'} +" + }, + run [| "update"; "-i"; "Examples\\SyntaxError.qs" |] "" + ) + [] let ``Shows file not found error`` () = let result = run [| "update"; "-i"; "Examples\\NotFound.qs" |] "" From cc455a74a4d1566ae1e356ded158bcabd171e32c Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 13:18:58 -0700 Subject: [PATCH 22/35] fantomas --- src/QsFmt/App.Tests/Tests.fs | 4 +++- src/QsFmt/App/Program.fs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 66cb73bf60..4a14c42b16 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -158,7 +158,9 @@ let ``Shows syntax errors`` () = { Code = 1 Out = "" - Error = standardizeNewLines "Examples\SyntaxError.qs, Line 1, Character 16: mismatched input 'invalid' expecting {'function', 'internal', 'newtype', 'open', 'operation', '@', '}'} + Error = + standardizeNewLines + "Examples\SyntaxError.qs, Line 1, Character 16: mismatched input 'invalid' expecting {'function', 'internal', 'newtype', 'open', 'operation', '@', '}'} " }, run [| "update"; "-i"; "Examples\\SyntaxError.qs" |] "" diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index 5a41fdb0e0..7d1fbd325b 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -120,8 +120,7 @@ let main args = finally Directory.SetCurrentDirectory(cwd) with - | _ -> - () + | _ -> () let result = CommandLine.Parser.Default.ParseArguments args From 83fe02e65c8917867102e718952d15b55d92e10a Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 14:09:17 -0700 Subject: [PATCH 23/35] fixed fsi --- src/QsFmt/App/CommandLineArguments.fsi | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/QsFmt/App/CommandLineArguments.fsi b/src/QsFmt/App/CommandLineArguments.fsi index 0c0b059891..386c905aa2 100644 --- a/src/QsFmt/App/CommandLineArguments.fsi +++ b/src/QsFmt/App/CommandLineArguments.fsi @@ -8,7 +8,6 @@ module Microsoft.Quantum.QsFmt.App.Arguments open CommandLine.Text /// Object for capturing the arguments used with the `format` command. - [] type FormatArguments = { /// Flag to indicate if the `--backup` option was specified. @@ -31,7 +30,6 @@ module Microsoft.Quantum.QsFmt.App.Arguments static member examples : seq /// Object for capturing the arguments used with the `update` command. - [] type UpdateArguments = { /// Flag to indicate if the `--backup` option was specified. From 664d3e6b11c7f7adbd321aaae6647ce398ab8e22 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 22:23:11 -0700 Subject: [PATCH 24/35] Added tests for project handling. --- src/QsFmt/App.Tests/App.Tests.fsproj | 101 +++++++++++++++++- .../SimpleApplication/Excluded1.qs | 1 + .../SimpleApplication/Included1.qs | 1 + .../TestProjects/SimpleApplication/Program.qs | 11 ++ .../QSharpApplication1.csproj | 18 ++++ .../SimpleApplication/SubFolder1/Excluded2.qs | 1 + .../SimpleApplication/SubFolder1/Included2.qs | 1 + .../SubFolder1/SubSubFolder/Excluded4.qs | 1 + .../SubFolder1/SubSubFolder/Included4.qs | 1 + .../SimpleApplication/SubFolder2/Excluded3.qs | 1 + .../SimpleApplication/SubFolder2/Included3.qs | 1 + .../TestProjects/SimpleLibrary/Excluded1.qs | 1 + .../TestProjects/SimpleLibrary/Included1.qs | 1 + .../TestProjects/SimpleLibrary/Library.qs | 10 ++ .../SimpleLibrary/QSharpLibrary1.csproj | 17 +++ .../SimpleLibrary/SubFolder1/Excluded2.qs | 1 + .../SimpleLibrary/SubFolder1/Included2.qs | 1 + .../SubFolder1/SubSubFolder/Excluded4.qs | 1 + .../SubFolder1/SubSubFolder/Included4.qs | 1 + .../SimpleLibrary/SubFolder2/Excluded3.qs | 1 + .../SimpleLibrary/SubFolder2/Included3.qs | 1 + .../SimpleTestProject/Excluded1.qs | 1 + .../SimpleTestProject/Included1.qs | 1 + .../QSharpTestProject1.csproj | 26 +++++ .../SimpleTestProject/SubFolder1/Excluded2.qs | 1 + .../SimpleTestProject/SubFolder1/Included2.qs | 1 + .../SubFolder1/SubSubFolder/Excluded4.qs | 1 + .../SubFolder1/SubSubFolder/Included4.qs | 1 + .../SimpleTestProject/SubFolder2/Excluded3.qs | 1 + .../SimpleTestProject/SubFolder2/Included3.qs | 1 + .../TestProjects/SimpleTestProject/Tests.qs | 15 +++ src/QsFmt/App.Tests/ProjectTests.fs | 74 +++++++++++++ src/QsFmt/App.Tests/Tests.fs | 26 ++--- src/QsFmt/App/App.fsproj | 1 + src/QsFmt/App/AssemblyInfo.fs | 6 ++ src/QsFmt/App/DesignTimeBuild.fs | 27 +++++ src/QsFmt/App/DesignTimeBuild.fsi | 3 + src/QsFmt/App/Program.fs | 28 +---- 38 files changed, 345 insertions(+), 42 deletions(-) create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Excluded1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Included1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Excluded2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Included2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Excluded4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Included4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Excluded3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Included3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Excluded1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Included1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Excluded2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Included2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Excluded4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Included4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Excluded3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Included3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Excluded1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Included1.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Excluded2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Included2.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Excluded4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Included4.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Excluded3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Included3.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs create mode 100644 src/QsFmt/App.Tests/ProjectTests.fs create mode 100644 src/QsFmt/App/AssemblyInfo.fs diff --git a/src/QsFmt/App.Tests/App.Tests.fsproj b/src/QsFmt/App.Tests/App.Tests.fsproj index 7920638d83..9588282f7f 100644 --- a/src/QsFmt/App.Tests/App.Tests.fsproj +++ b/src/QsFmt/App.Tests/App.Tests.fsproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 Microsoft.Quantum.QsFmt.App.Tests @@ -7,9 +7,11 @@ - PreserveNewest + Always + + + Always - Always @@ -43,9 +45,102 @@ Always + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Excluded1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Excluded1.qs new file mode 100644 index 0000000000..2239054759 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Excluded1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Included1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Included1.qs new file mode 100644 index 0000000000..9ea3b00a59 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Included1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs new file mode 100644 index 0000000000..c22d3f4148 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs @@ -0,0 +1,11 @@ +namespace Quantum.QSharpApplication1 { + + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + + + @EntryPoint() + operation HelloQ () : Unit { + Message("Hello quantum world!"); + } +} diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj new file mode 100644 index 0000000000..bb75b849f1 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj @@ -0,0 +1,18 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + Example1.qs + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Excluded2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Excluded2.qs new file mode 100644 index 0000000000..6d39319e6f --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Excluded2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Included2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Included2.qs new file mode 100644 index 0000000000..e43f0a6bb8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/Included2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Excluded4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Excluded4.qs new file mode 100644 index 0000000000..25e12245a9 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Excluded4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Included4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Included4.qs new file mode 100644 index 0000000000..f287e29711 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder1/SubSubFolder/Included4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Excluded3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Excluded3.qs new file mode 100644 index 0000000000..6c610f8de8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Excluded3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Included3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Included3.qs new file mode 100644 index 0000000000..0485b054ee --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/SubFolder2/Included3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Excluded1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Excluded1.qs new file mode 100644 index 0000000000..2239054759 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Excluded1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Included1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Included1.qs new file mode 100644 index 0000000000..9ea3b00a59 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Included1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs new file mode 100644 index 0000000000..af12f3da18 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs @@ -0,0 +1,10 @@ +namespace Quantum.QSharpLibrary1 { + + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + + + operation HelloQ () : Unit { + Message("Hello quantum world!"); + } +} diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj new file mode 100644 index 0000000000..85f7c6741f --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj @@ -0,0 +1,17 @@ + + + + netstandard2.1 + + + + + + + + + Example1.qs + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Excluded2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Excluded2.qs new file mode 100644 index 0000000000..6d39319e6f --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Excluded2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Included2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Included2.qs new file mode 100644 index 0000000000..e43f0a6bb8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/Included2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Excluded4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Excluded4.qs new file mode 100644 index 0000000000..25e12245a9 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Excluded4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Included4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Included4.qs new file mode 100644 index 0000000000..f287e29711 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder1/SubSubFolder/Included4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Excluded3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Excluded3.qs new file mode 100644 index 0000000000..6c610f8de8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Excluded3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Included3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Included3.qs new file mode 100644 index 0000000000..0485b054ee --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/SubFolder2/Included3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Excluded1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Excluded1.qs new file mode 100644 index 0000000000..2239054759 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Excluded1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Included1.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Included1.qs new file mode 100644 index 0000000000..9ea3b00a59 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Included1.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included1() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj new file mode 100644 index 0000000000..2e0f7ac47b --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj @@ -0,0 +1,26 @@ + + + + netcoreapp3.1 + false + + + + + + + + + Example1.qs + + + + + + + + + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Excluded2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Excluded2.qs new file mode 100644 index 0000000000..6d39319e6f --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Excluded2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Included2.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Included2.qs new file mode 100644 index 0000000000..e43f0a6bb8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/Included2.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included2() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Excluded4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Excluded4.qs new file mode 100644 index 0000000000..25e12245a9 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Excluded4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Included4.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Included4.qs new file mode 100644 index 0000000000..f287e29711 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder1/SubSubFolder/Included4.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included4() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Excluded3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Excluded3.qs new file mode 100644 index 0000000000..6c610f8de8 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Excluded3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Excluded3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Included3.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Included3.qs new file mode 100644 index 0000000000..0485b054ee --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/SubFolder2/Included3.qs @@ -0,0 +1 @@ +namespace TestTarget { function Included3() : Int { for (i in 0..1) {} return 0; } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs new file mode 100644 index 0000000000..d4bb5d394b --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs @@ -0,0 +1,15 @@ +namespace Quantum.QSharpTestProject1 { + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.Intrinsic; + + + @Test("QuantumSimulator") + operation AllocateQubit () : Unit { + + use q = Qubit(); + AssertMeasurement([PauliZ], [q], Zero, "Newly allocated qubit must be in |0> state."); + + Message("Test passed."); + } +} diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs new file mode 100644 index 0000000000..f7d58d84ab --- /dev/null +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -0,0 +1,74 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +module Microsoft.Quantum.QsFmt.App.ProjectTests + +open System.IO +open Microsoft.Quantum.QsFmt.App +open Xunit + +[] +let SimpleApplication () = + DesignTimeBuild.initiate() + + let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleApplication\QSharpApplication1.csproj" + let files = files |> List.map Path.GetFullPath + let expectedFiles = + [ + "Examples\TestProjects\SimpleApplication\Program.qs" + "Examples\TestProjects\SimpleApplication\Included1.qs" + "Examples\TestProjects\SimpleApplication\SubFolder1\Included2.qs" + "Examples\TestProjects\SimpleApplication\SubFolder2\Included3.qs" + "Examples\TestProjects\SimpleApplication\SubFolder1\SubSubFolder\Included4.qs" + "Examples\Example1.qs" + ] + |> List.map Path.GetFullPath + + Assert.True(expectedFiles.Length = files.Length, "Didn't get the expected number of files.") + + for expected in expectedFiles do + Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) + +[] +let SimpleLibrary () = + DesignTimeBuild.initiate() + + let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleLibrary\QSharpLibrary1.csproj" + let files = files |> List.map Path.GetFullPath + let expectedFiles = + [ + "Examples\TestProjects\SimpleLibrary\Library.qs" + "Examples\TestProjects\SimpleLibrary\Included1.qs" + "Examples\TestProjects\SimpleLibrary\SubFolder1\Included2.qs" + "Examples\TestProjects\SimpleLibrary\SubFolder2\Included3.qs" + "Examples\TestProjects\SimpleLibrary\SubFolder1\SubSubFolder\Included4.qs" + "Examples\Example1.qs" + ] + |> List.map Path.GetFullPath + + Assert.True(expectedFiles.Length = files.Length, "Didn't get the expected number of files.") + + for expected in expectedFiles do + Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) + +[] +let SimpleTestProject () = + DesignTimeBuild.initiate() + + let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleTestProject\QSharpTestProject1.csproj" + let files = files |> List.map Path.GetFullPath + let expectedFiles = + [ + "Examples\TestProjects\SimpleTestProject\Tests.qs" + "Examples\TestProjects\SimpleTestProject\Included1.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder1\Included2.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder2\Included3.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder1\SubSubFolder\Included4.qs" + "Examples\Example1.qs" + ] + |> List.map Path.GetFullPath + + Assert.True(expectedFiles.Length = files.Length, "Didn't get the expected number of files.") + + for expected in expectedFiles do + Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 4a14c42b16..76860f8920 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -10,7 +10,7 @@ open Microsoft.Quantum.QsFmt.App.Program open Xunit /// The result of running the application. -type Result = +type private Result = { /// The exit status code. Code: int @@ -22,7 +22,7 @@ type Result = Error: string } -type TestFile = +type private TestFile = { Path: string Original: string @@ -33,17 +33,17 @@ type TestFile = /// /// Ensures that the new line characters will conform to the standard of the environment's new line character. /// -let standardizeNewLines (s: string) = +let private standardizeNewLines (s: string) = s.Replace("\r", "").Replace("\n", Environment.NewLine) -let CleanResult = +let private CleanResult = { Code = 0 Out = "" Error = "" } -let makeTestFile (path: string) = +let private makeTestFile (path: string) = let name = path.[(path.LastIndexOf "\\") + 1..(path.LastIndexOf ".qs") - 1] { @@ -68,15 +68,15 @@ let makeTestFile (path: string) = |> standardizeNewLines } -let Example1 = makeTestFile "Examples\\Example1.qs" -let Example2 = makeTestFile "Examples\\Example2.qs" -let SubExample1 = makeTestFile "Examples\\SubExamples1\\SubExample1.qs" -let SubExample2 = makeTestFile "Examples\\SubExamples1\\SubExample2.qs" -let SubExample3 = makeTestFile "Examples\\SubExamples2\\SubExample3.qs" -let NestedExample1 = makeTestFile "Examples\\SubExamples2\\NestedExamples\\NestedExample1.qs" -let NestedExample2 = makeTestFile "Examples\\SubExamples2\\NestedExamples\\NestedExample2.qs" +let private Example1 = makeTestFile "Examples\\Example1.qs" +let private Example2 = makeTestFile "Examples\\Example2.qs" +let private SubExample1 = makeTestFile "Examples\\SubExamples1\\SubExample1.qs" +let private SubExample2 = makeTestFile "Examples\\SubExamples1\\SubExample2.qs" +let private SubExample3 = makeTestFile "Examples\\SubExamples2\\SubExample3.qs" +let private NestedExample1 = makeTestFile "Examples\\SubExamples2\\NestedExamples\\NestedExample1.qs" +let private NestedExample2 = makeTestFile "Examples\\SubExamples2\\NestedExamples\\NestedExample2.qs" -let StandardInputTest = +let private StandardInputTest = { Path = "-" Original = diff --git a/src/QsFmt/App/App.fsproj b/src/QsFmt/App/App.fsproj index bd9b231318..d3a15f89bf 100644 --- a/src/QsFmt/App/App.fsproj +++ b/src/QsFmt/App/App.fsproj @@ -7,6 +7,7 @@ + diff --git a/src/QsFmt/App/AssemblyInfo.fs b/src/QsFmt/App/AssemblyInfo.fs new file mode 100644 index 0000000000..052663fa59 --- /dev/null +++ b/src/QsFmt/App/AssemblyInfo.fs @@ -0,0 +1,6 @@ +module AssemblyInfo + +open System.Runtime.CompilerServices + +[] +do() diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index 06f2300a7d..e53605f997 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -6,8 +6,10 @@ module Microsoft.Quantum.QsFmt.App.DesignTimeBuild open System open System.Collections.Generic open System.IO +open System.Runtime.Loader open Microsoft.Build.Evaluation open Microsoft.Build.Execution +open Microsoft.Build.Locator open Microsoft.Build.Utilities let globalProperties = @@ -63,3 +65,28 @@ let getSourceFiles (projectFile: string) = finally if not (isNull project || isNull ProjectCollection.GlobalProjectCollection) then ProjectCollection.GlobalProjectCollection.UnloadProject project + +let initiate () = + // We need to set the current directory to the same directory of + // the LanguageServer executable so that it will pick the global.json file + // and force the MSBuildLocator to use .NET Core SDK 3.1 + let cwd = Directory.GetCurrentDirectory() + Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory) + // In the case where we actually instantiate a server, we need to "configure" the design time build. + // This needs to be done before any MsBuild packages are loaded. + try + try + let vsi = MSBuildLocator.RegisterDefaults() + + // We're using the installed version of the binaries to avoid a dependency between + // the .NET Core SDK version and NuGet. This is a workaround due to the issue below: + // https://github.com/microsoft/MSBuildLocator/issues/86 + AssemblyLoadContext.Default.add_Resolving ( + new Func<_, _, _>(fun assemblyLoadContext assemblyName -> + let path = Path.Combine(vsi.MSBuildPath, sprintf "%s.dll" assemblyName.Name) + if File.Exists(path) then assemblyLoadContext.LoadFromAssemblyPath path else null) + ) + finally + Directory.SetCurrentDirectory(cwd) + with + | _ -> () diff --git a/src/QsFmt/App/DesignTimeBuild.fsi b/src/QsFmt/App/DesignTimeBuild.fsi index 2f003afc59..c8c9b03545 100644 --- a/src/QsFmt/App/DesignTimeBuild.fsi +++ b/src/QsFmt/App/DesignTimeBuild.fsi @@ -6,3 +6,6 @@ module internal Microsoft.Quantum.QsFmt.App.DesignTimeBuild /// Given a path to a project file, returns the list of files associated /// to the project and the Quantum SDK version if found. val getSourceFiles : string -> string list * string option + + /// Initializes the ??? // TODO + val initiate : Unit -> Unit diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index 7d1fbd325b..de2541363d 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -6,10 +6,9 @@ module Microsoft.Quantum.QsFmt.App.Program open System open System.Collections.Generic open System.IO -open System.Runtime.Loader open CommandLine -open Microsoft.Build.Locator open Microsoft.Quantum.QsFmt.App.Arguments +open Microsoft.Quantum.QsFmt.App.DesignTimeBuild open Microsoft.Quantum.QsFmt.Formatter let makeFullPath input = @@ -98,30 +97,7 @@ let runFormat (arguments: FormatArguments) = [] let main args = - // We need to set the current directory to the same directory of - // the LanguageServer executable so that it will pick the global.json file - // and force the MSBuildLocator to use .NET Core SDK 3.1 - let cwd = Directory.GetCurrentDirectory() - Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory) - // In the case where we actually instantiate a server, we need to "configure" the design time build. - // This needs to be done before any MsBuild packages are loaded. - try - try - let vsi = MSBuildLocator.RegisterDefaults() - - // We're using the installed version of the binaries to avoid a dependency between - // the .NET Core SDK version and NuGet. This is a workaround due to the issue below: - // https://github.com/microsoft/MSBuildLocator/issues/86 - AssemblyLoadContext.Default.add_Resolving ( - new Func<_, _, _>(fun assemblyLoadContext assemblyName -> - let path = Path.Combine(vsi.MSBuildPath, sprintf "%s.dll" assemblyName.Name) - if File.Exists(path) then assemblyLoadContext.LoadFromAssemblyPath path else null) - ) - finally - Directory.SetCurrentDirectory(cwd) - with - | _ -> () - + initiate() let result = CommandLine.Parser.Default.ParseArguments args From f942b07f484f7acd742f036c27ddad53d1b10a91 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 22:26:12 -0700 Subject: [PATCH 25/35] Changed name --- src/QsFmt/App.Tests/ProjectTests.fs | 6 +++--- src/QsFmt/App/DesignTimeBuild.fs | 2 +- src/QsFmt/App/DesignTimeBuild.fsi | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs index f7d58d84ab..7c50ed1885 100644 --- a/src/QsFmt/App.Tests/ProjectTests.fs +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -9,7 +9,7 @@ open Xunit [] let SimpleApplication () = - DesignTimeBuild.initiate() + DesignTimeBuild.assemblyLoadContextSetup() let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleApplication\QSharpApplication1.csproj" let files = files |> List.map Path.GetFullPath @@ -31,7 +31,7 @@ let SimpleApplication () = [] let SimpleLibrary () = - DesignTimeBuild.initiate() + DesignTimeBuild.assemblyLoadContextSetup() let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleLibrary\QSharpLibrary1.csproj" let files = files |> List.map Path.GetFullPath @@ -53,7 +53,7 @@ let SimpleLibrary () = [] let SimpleTestProject () = - DesignTimeBuild.initiate() + DesignTimeBuild.assemblyLoadContextSetup() let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleTestProject\QSharpTestProject1.csproj" let files = files |> List.map Path.GetFullPath diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index e53605f997..eb639331f9 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -66,7 +66,7 @@ let getSourceFiles (projectFile: string) = if not (isNull project || isNull ProjectCollection.GlobalProjectCollection) then ProjectCollection.GlobalProjectCollection.UnloadProject project -let initiate () = +let assemblyLoadContextSetup () = // We need to set the current directory to the same directory of // the LanguageServer executable so that it will pick the global.json file // and force the MSBuildLocator to use .NET Core SDK 3.1 diff --git a/src/QsFmt/App/DesignTimeBuild.fsi b/src/QsFmt/App/DesignTimeBuild.fsi index c8c9b03545..4e598d73cc 100644 --- a/src/QsFmt/App/DesignTimeBuild.fsi +++ b/src/QsFmt/App/DesignTimeBuild.fsi @@ -7,5 +7,5 @@ module internal Microsoft.Quantum.QsFmt.App.DesignTimeBuild /// to the project and the Quantum SDK version if found. val getSourceFiles : string -> string list * string option - /// Initializes the ??? // TODO - val initiate : Unit -> Unit + /// Initializes the assembly load context needed for building project files. + val assemblyLoadContextSetup : Unit -> Unit From cdc167a5ff239cb501c07b28bc3b433d6fbcaad2 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Tue, 12 Oct 2021 22:38:07 -0700 Subject: [PATCH 26/35] fantomas --- src/QsFmt/App.Tests/ProjectTests.fs | 17 ++++++++++++----- src/QsFmt/App/AssemblyInfo.fs | 2 +- src/QsFmt/App/Program.fs | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs index 7c50ed1885..8cece9cf9f 100644 --- a/src/QsFmt/App.Tests/ProjectTests.fs +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -9,10 +9,13 @@ open Xunit [] let SimpleApplication () = - DesignTimeBuild.assemblyLoadContextSetup() + DesignTimeBuild.assemblyLoadContextSetup () + + let files, version = + DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleApplication\QSharpApplication1.csproj" - let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleApplication\QSharpApplication1.csproj" let files = files |> List.map Path.GetFullPath + let expectedFiles = [ "Examples\TestProjects\SimpleApplication\Program.qs" @@ -31,10 +34,11 @@ let SimpleApplication () = [] let SimpleLibrary () = - DesignTimeBuild.assemblyLoadContextSetup() + DesignTimeBuild.assemblyLoadContextSetup () let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleLibrary\QSharpLibrary1.csproj" let files = files |> List.map Path.GetFullPath + let expectedFiles = [ "Examples\TestProjects\SimpleLibrary\Library.qs" @@ -53,10 +57,13 @@ let SimpleLibrary () = [] let SimpleTestProject () = - DesignTimeBuild.assemblyLoadContextSetup() + DesignTimeBuild.assemblyLoadContextSetup () + + let files, version = + DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleTestProject\QSharpTestProject1.csproj" - let files, version = DesignTimeBuild.getSourceFiles "Examples\TestProjects\SimpleTestProject\QSharpTestProject1.csproj" let files = files |> List.map Path.GetFullPath + let expectedFiles = [ "Examples\TestProjects\SimpleTestProject\Tests.qs" diff --git a/src/QsFmt/App/AssemblyInfo.fs b/src/QsFmt/App/AssemblyInfo.fs index 052663fa59..8422068d78 100644 --- a/src/QsFmt/App/AssemblyInfo.fs +++ b/src/QsFmt/App/AssemblyInfo.fs @@ -3,4 +3,4 @@ open System.Runtime.CompilerServices [] -do() +do () diff --git a/src/QsFmt/App/Program.fs b/src/QsFmt/App/Program.fs index de2541363d..ebbe189fa1 100644 --- a/src/QsFmt/App/Program.fs +++ b/src/QsFmt/App/Program.fs @@ -97,7 +97,7 @@ let runFormat (arguments: FormatArguments) = [] let main args = - initiate() + assemblyLoadContextSetup () let result = CommandLine.Parser.Default.ParseArguments args From 1d6a7584d4fb5a92c68700b6d157481612ae7f61 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 13 Oct 2021 11:57:54 -0700 Subject: [PATCH 27/35] Added reference to external file to test projects. --- .../TestProjects/SimpleApplication/QSharpApplication1.csproj | 1 + .../Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj | 1 + .../TestProjects/SimpleTestProject/QSharpTestProject1.csproj | 1 + src/QsFmt/App.Tests/ProjectTests.fs | 3 +++ 4 files changed, 6 insertions(+) diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj index bb75b849f1..9dde6585fd 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj @@ -13,6 +13,7 @@ Example1.qs + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj index 85f7c6741f..94a176c6ea 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj @@ -12,6 +12,7 @@ Example1.qs + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj index 2e0f7ac47b..857a0e8c0a 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj @@ -13,6 +13,7 @@ Example1.qs + diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs index 8cece9cf9f..d42d3678c4 100644 --- a/src/QsFmt/App.Tests/ProjectTests.fs +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -24,6 +24,7 @@ let SimpleApplication () = "Examples\TestProjects\SimpleApplication\SubFolder2\Included3.qs" "Examples\TestProjects\SimpleApplication\SubFolder1\SubSubFolder\Included4.qs" "Examples\Example1.qs" + "Examples\Example2.qs" ] |> List.map Path.GetFullPath @@ -47,6 +48,7 @@ let SimpleLibrary () = "Examples\TestProjects\SimpleLibrary\SubFolder2\Included3.qs" "Examples\TestProjects\SimpleLibrary\SubFolder1\SubSubFolder\Included4.qs" "Examples\Example1.qs" + "Examples\Example2.qs" ] |> List.map Path.GetFullPath @@ -72,6 +74,7 @@ let SimpleTestProject () = "Examples\TestProjects\SimpleTestProject\SubFolder2\Included3.qs" "Examples\TestProjects\SimpleTestProject\SubFolder1\SubSubFolder\Included4.qs" "Examples\Example1.qs" + "Examples\Example2.qs" ] |> List.map Path.GetFullPath From bdb6e53b6329dd532d433249a74a64a53215202a Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 13 Oct 2021 12:22:19 -0700 Subject: [PATCH 28/35] Added project references to test projects. --- src/QsFmt/App.Tests/App.Tests.fsproj | 6 ++++++ .../Examples/TestProjects/ReferenceLibrary/Library.qs | 10 ++++++++++ .../ReferenceLibrary/ReferenceLibrary.csproj | 7 +++++++ .../Examples/TestProjects/SimpleApplication/Program.qs | 2 ++ .../SimpleApplication/QSharpApplication1.csproj | 4 ++++ .../Examples/TestProjects/SimpleLibrary/Library.qs | 2 ++ .../TestProjects/SimpleLibrary/QSharpLibrary1.csproj | 4 ++++ .../SimpleTestProject/QSharpTestProject1.csproj | 4 ++++ .../Examples/TestProjects/SimpleTestProject/Tests.qs | 2 ++ 9 files changed, 41 insertions(+) create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/Library.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/ReferenceLibrary.csproj diff --git a/src/QsFmt/App.Tests/App.Tests.fsproj b/src/QsFmt/App.Tests/App.Tests.fsproj index 9588282f7f..5096b6b783 100644 --- a/src/QsFmt/App.Tests/App.Tests.fsproj +++ b/src/QsFmt/App.Tests/App.Tests.fsproj @@ -135,6 +135,12 @@ Always + + Always + + + Always + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/Library.qs b/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/Library.qs new file mode 100644 index 0000000000..650943fa09 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/Library.qs @@ -0,0 +1,10 @@ +namespace Quantum.ReferenceLibrary { + + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + + + operation LibraryOperation () : Unit { + Message("Hello from the Reference Library!"); + } +} diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/ReferenceLibrary.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/ReferenceLibrary.csproj new file mode 100644 index 0000000000..39db37413f --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/ReferenceLibrary/ReferenceLibrary.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.1 + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs index c22d3f4148..d1cd4bbf2d 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs @@ -2,10 +2,12 @@ open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; + open Quantum.ReferenceLibrary; @EntryPoint() operation HelloQ () : Unit { Message("Hello quantum world!"); + LibraryOperation(); } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj index 9dde6585fd..7d8f9c1b15 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs index af12f3da18..f7a5b03e3e 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs @@ -2,9 +2,11 @@ open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; + open Quantum.ReferenceLibrary; operation HelloQ () : Unit { Message("Hello quantum world!"); + LibraryOperation(); } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj index 94a176c6ea..1e0216380b 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj @@ -15,4 +15,8 @@ + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj index 857a0e8c0a..6b2c2cfb5b 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj @@ -16,6 +16,10 @@ + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs index d4bb5d394b..32b964ab7b 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs @@ -2,6 +2,7 @@ open Microsoft.Quantum.Canon; open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Intrinsic; + open Quantum.ReferenceLibrary; @Test("QuantumSimulator") @@ -11,5 +12,6 @@ AssertMeasurement([PauliZ], [q], Zero, "Newly allocated qubit must be in |0> state."); Message("Test passed."); + LibraryOperation(); } } From 42588906b07b6dc128651ca8670bfb0f001435fb Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 13 Oct 2021 14:44:52 -0700 Subject: [PATCH 29/35] Prevent qdk versions older than 0.16.2104.138035 from being updated. --- src/QsFmt/App/CommandLineArguments.fs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/QsFmt/App/CommandLineArguments.fs b/src/QsFmt/App/CommandLineArguments.fs index 9b9f458995..2f8a1f1d1b 100644 --- a/src/QsFmt/App/CommandLineArguments.fs +++ b/src/QsFmt/App/CommandLineArguments.fs @@ -169,14 +169,19 @@ module Arguments = | Some s -> Version.Parse s |> Some | None -> None - { - CommandKind = Update - RecurseFlag = arguments.Recurse - BackupFlag = arguments.Backup - QSharp_Version = qsharp_version - Inputs = inputs - } - |> Result.Ok + match qsharp_version with + | Some v when v < Version("0.16.2104.138035") -> + eprintfn "Error: Qdk Version is out of date. Only Qdk version 0.16.2104.138035 or later is supported." + 6 |> Result.Error + | _ -> + { + CommandKind = Update + RecurseFlag = arguments.Recurse + BackupFlag = arguments.Backup + QSharp_Version = qsharp_version + Inputs = inputs + } + |> Result.Ok else for e in errors do eprintfn "%s" e From ed876adcbd13ab228c9beb0b3864adb9c3191612 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 13 Oct 2021 16:17:33 -0700 Subject: [PATCH 30/35] Added test for erroring on older versions of the Qdk. The OldApplication test is still WIP. --- src/QsFmt/App.Tests/App.Tests.fsproj | 15 +++++++++++ .../TestProjects/OldApplication/Driver.cs | 13 ++++++++++ .../OldApplication/OldApplication.csproj | 14 ++++++++++ .../TestProjects/OldApplication/Operation.qs | 7 +++++ .../TestProjects/OldVersion/OldVersion.csproj | 8 ++++++ .../TestProjects/OldVersion/Program.qs | 11 ++++++++ src/QsFmt/App.Tests/ProjectTests.fs | 26 +++++++++++++++++++ src/QsFmt/App.Tests/Tests.fs | 14 ++++++++++ 8 files changed, 108 insertions(+) create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Driver.cs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/OldApplication.csproj create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Operation.qs create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/OldVersion.csproj create mode 100644 src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/Program.qs diff --git a/src/QsFmt/App.Tests/App.Tests.fsproj b/src/QsFmt/App.Tests/App.Tests.fsproj index 5096b6b783..363bcd250c 100644 --- a/src/QsFmt/App.Tests/App.Tests.fsproj +++ b/src/QsFmt/App.Tests/App.Tests.fsproj @@ -141,6 +141,21 @@ Always + + Always + + + Always + + + Always + + + Always + + + Always + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Driver.cs b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Driver.cs new file mode 100644 index 0000000000..dffbcc8788 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Driver.cs @@ -0,0 +1,13 @@ +using Microsoft.Quantum.Simulation.Core; +using Microsoft.Quantum.Simulation.Simulators; + +namespace OldApplication +{ + class Driver + { + static void Main(string[] args) + { + + } + } +} diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/OldApplication.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/OldApplication.csproj new file mode 100644 index 0000000000..c2c94b133c --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/OldApplication.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + x64 + + + + + + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Operation.qs b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Operation.qs new file mode 100644 index 0000000000..5f51e12592 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/OldApplication/Operation.qs @@ -0,0 +1,7 @@ +namespace OldApplication { + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + + operation Operation () : Unit { + } +} diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/OldVersion.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/OldVersion.csproj new file mode 100644 index 0000000000..ffac44e123 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/OldVersion.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/Program.qs b/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/Program.qs new file mode 100644 index 0000000000..c22d3f4148 --- /dev/null +++ b/src/QsFmt/App.Tests/Examples/TestProjects/OldVersion/Program.qs @@ -0,0 +1,11 @@ +namespace Quantum.QSharpApplication1 { + + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + + + @EntryPoint() + operation HelloQ () : Unit { + Message("Hello quantum world!"); + } +} diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs index d42d3678c4..7d43d2d468 100644 --- a/src/QsFmt/App.Tests/ProjectTests.fs +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -82,3 +82,29 @@ let SimpleTestProject () = for expected in expectedFiles do Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) + +[] +let OldApplication () = + DesignTimeBuild.assemblyLoadContextSetup () + + let files, version = + DesignTimeBuild.getSourceFiles "Examples\TestProjects\OldApplication\OldApplication.csproj" + + let files = files |> List.map Path.GetFullPath + + let expectedFiles = + [ + "Examples\TestProjects\SimpleTestProject\Tests.qs" + "Examples\TestProjects\SimpleTestProject\Included1.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder1\Included2.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder2\Included3.qs" + "Examples\TestProjects\SimpleTestProject\SubFolder1\SubSubFolder\Included4.qs" + "Examples\Example1.qs" + "Examples\Example2.qs" + ] + |> List.map Path.GetFullPath + + Assert.True(expectedFiles.Length = files.Length, "Didn't get the expected number of files.") + + for expected in expectedFiles do + Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 76860f8920..fb8829fd27 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -340,3 +340,17 @@ let ``Project file as input`` () = let excluded2 = File.ReadAllText TestTargetExcluded2.Path Assert.Equal(excluded2, TestTargetExcluded2.Original) + +[] +let ``Old version project file as input`` () = + Assert.Equal( + { + Code = 6 + Out = "" + Error = + standardizeNewLines + "Error: Qdk Version is out of date. Only Qdk version 0.16.2104.138035 or later is supported. +" + }, + run [| "update"; "-p"; "Examples\\TestProjects\\OldVersion\\OldVersion.csproj" |] "" + ) From 381f963c268ef9e1c7869765b0076a228271d82c Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 14 Oct 2021 12:50:50 -0700 Subject: [PATCH 31/35] Not sure yet how to discriminate between old and current types of projects. --- src/QsFmt/App.Tests/ProjectTests.fs | 26 -------------------------- src/QsFmt/App.Tests/Tests.fs | 10 +++++++++- src/QsFmt/App/DesignTimeBuild.fs | 1 - 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/QsFmt/App.Tests/ProjectTests.fs b/src/QsFmt/App.Tests/ProjectTests.fs index 7d43d2d468..d42d3678c4 100644 --- a/src/QsFmt/App.Tests/ProjectTests.fs +++ b/src/QsFmt/App.Tests/ProjectTests.fs @@ -82,29 +82,3 @@ let SimpleTestProject () = for expected in expectedFiles do Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) - -[] -let OldApplication () = - DesignTimeBuild.assemblyLoadContextSetup () - - let files, version = - DesignTimeBuild.getSourceFiles "Examples\TestProjects\OldApplication\OldApplication.csproj" - - let files = files |> List.map Path.GetFullPath - - let expectedFiles = - [ - "Examples\TestProjects\SimpleTestProject\Tests.qs" - "Examples\TestProjects\SimpleTestProject\Included1.qs" - "Examples\TestProjects\SimpleTestProject\SubFolder1\Included2.qs" - "Examples\TestProjects\SimpleTestProject\SubFolder2\Included3.qs" - "Examples\TestProjects\SimpleTestProject\SubFolder1\SubSubFolder\Included4.qs" - "Examples\Example1.qs" - "Examples\Example2.qs" - ] - |> List.map Path.GetFullPath - - Assert.True(expectedFiles.Length = files.Length, "Didn't get the expected number of files.") - - for expected in expectedFiles do - Assert.True(List.contains expected files, sprintf "Expected but did not find file %s" expected) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index fb8829fd27..4bf4ec83db 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -342,7 +342,7 @@ let ``Project file as input`` () = Assert.Equal(excluded2, TestTargetExcluded2.Original) [] -let ``Old version project file as input`` () = +let ``Outdated version project file as input`` () = Assert.Equal( { Code = 6 @@ -354,3 +354,11 @@ let ``Old version project file as input`` () = }, run [| "update"; "-p"; "Examples\\TestProjects\\OldVersion\\OldVersion.csproj" |] "" ) + +// ToDo +[] +let ``Outdated system project file as input`` () = + let project = "Examples\\TestProjects\\OldApplication\\OldApplication.csproj" + let act = Action(fun () -> run [| "update"; "-p"; project |] "" |> ignore) + let sysException = Assert.Throws(act) + () diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index eb639331f9..dc3a90c11a 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -48,7 +48,6 @@ let getSourceFiles (projectFile: string) = project <- new Project(projectFile, properties, ToolLocationHelper.CurrentToolsVersion) let instance = project.CreateProjectInstance() - // do we want to check if the project is a Q# project and if not fail? if not <| instance.Targets.ContainsKey "QSharpCompile" then failwith (sprintf "The given project file, %s is not a Q# project file." projectFile) From e2914b28f8c3a1e3bb8d6992a8123cd2918267ff Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 14 Oct 2021 14:08:49 -0700 Subject: [PATCH 32/35] fantomas --- src/QsFmt/App.Tests/Tests.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 4bf4ec83db..1325e0c704 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -356,7 +356,7 @@ let ``Outdated version project file as input`` () = ) // ToDo -[] +[] let ``Outdated system project file as input`` () = let project = "Examples\\TestProjects\\OldApplication\\OldApplication.csproj" let act = Action(fun () -> run [| "update"; "-p"; project |] "" |> ignore) From 48fba9769790e470c2f5108fdb73b2899630e051 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 14 Oct 2021 16:19:49 -0700 Subject: [PATCH 33/35] Add package references to the test projects. --- .../Examples/TestProjects/SimpleApplication/Program.qs | 2 ++ .../TestProjects/SimpleApplication/QSharpApplication1.csproj | 1 + .../App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs | 2 ++ .../Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj | 1 + .../TestProjects/SimpleTestProject/QSharpTestProject1.csproj | 1 + .../App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs | 3 +++ 6 files changed, 10 insertions(+) diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs index d1cd4bbf2d..8e16620336 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/Program.qs @@ -1,6 +1,7 @@ namespace Quantum.QSharpApplication1 { open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Chemistry; open Microsoft.Quantum.Intrinsic; open Quantum.ReferenceLibrary; @@ -9,5 +10,6 @@ operation HelloQ () : Unit { Message("Hello quantum world!"); LibraryOperation(); + let x = HTerm([], []); } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj index 7d8f9c1b15..71a8f049d0 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleApplication/QSharpApplication1.csproj @@ -18,6 +18,7 @@ + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs index f7a5b03e3e..a1c9db7d4c 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/Library.qs @@ -1,6 +1,7 @@ namespace Quantum.QSharpLibrary1 { open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Chemistry; open Microsoft.Quantum.Intrinsic; open Quantum.ReferenceLibrary; @@ -8,5 +9,6 @@ operation HelloQ () : Unit { Message("Hello quantum world!"); LibraryOperation(); + let x = HTerm([], []); } } diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj index 1e0216380b..2d69cbcb3e 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleLibrary/QSharpLibrary1.csproj @@ -17,6 +17,7 @@ + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj index 6b2c2cfb5b..7a478df8c6 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/QSharpTestProject1.csproj @@ -18,6 +18,7 @@ + diff --git a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs index 32b964ab7b..d9eba8a6db 100644 --- a/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs +++ b/src/QsFmt/App.Tests/Examples/TestProjects/SimpleTestProject/Tests.qs @@ -1,5 +1,7 @@ namespace Quantum.QSharpTestProject1 { + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Chemistry; open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Intrinsic; open Quantum.ReferenceLibrary; @@ -13,5 +15,6 @@ Message("Test passed."); LibraryOperation(); + let x = HTerm([], []); } } From fcd8cc052cb09b4a17efefbac23e531e5d6bffc5 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 14 Oct 2021 16:46:44 -0700 Subject: [PATCH 34/35] fixed check and associated test for non-quantum.sdk projects. --- src/QsFmt/App.Tests/Tests.fs | 7 +++---- src/QsFmt/App/CommandLineArguments.fs | 7 ++----- src/QsFmt/App/DesignTimeBuild.fs | 9 +++------ src/QsFmt/App/DesignTimeBuild.fsi | 5 +++-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 1325e0c704..14d8e99049 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -355,10 +355,9 @@ let ``Outdated version project file as input`` () = run [| "update"; "-p"; "Examples\\TestProjects\\OldVersion\\OldVersion.csproj" |] "" ) -// ToDo -[] +[] let ``Outdated system project file as input`` () = let project = "Examples\\TestProjects\\OldApplication\\OldApplication.csproj" let act = Action(fun () -> run [| "update"; "-p"; project |] "" |> ignore) - let sysException = Assert.Throws(act) - () + let ex = Assert.Throws(act) + Assert.Equal(ex.Message, sprintf "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." project) diff --git a/src/QsFmt/App/CommandLineArguments.fs b/src/QsFmt/App/CommandLineArguments.fs index 2f8a1f1d1b..b7a216e56a 100644 --- a/src/QsFmt/App/CommandLineArguments.fs +++ b/src/QsFmt/App/CommandLineArguments.fs @@ -162,12 +162,9 @@ module Arguments = if isNull arguments.ProjectFile then arguments.InputFiles |> Seq.toList, arguments.QdkVersion |> Option.ofObj else - getSourceFiles arguments.ProjectFile + getSourceFiles arguments.ProjectFile |> (fun (i, v) -> (i, v |> Some)) - let qsharp_version = - match version with - | Some s -> Version.Parse s |> Some - | None -> None + let qsharp_version = version |> Option.map Version.Parse match qsharp_version with | Some v when v < Version("0.16.2104.138035") -> diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index dc3a90c11a..bbf063f4c2 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -48,16 +48,13 @@ let getSourceFiles (projectFile: string) = project <- new Project(projectFile, properties, ToolLocationHelper.CurrentToolsVersion) let instance = project.CreateProjectInstance() - if not <| instance.Targets.ContainsKey "QSharpCompile" then - failwith (sprintf "The given project file, %s is not a Q# project file." projectFile) - // get all the source files in the project let sourceFiles = getItemsByType instance "QSharpCompile" |> Seq.toList let version = - instance.Properties - |> Seq.tryFind (fun x -> x.Name = "QuantumSdkVersion") - |> Option.map (fun x -> x.EvaluatedValue) + match instance.Properties |> Seq.tryFind (fun x -> x.Name = "QuantumSdkVersion") with + | Some v -> v.EvaluatedValue + | _ -> failwith (sprintf "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." projectFile) (sourceFiles, version) diff --git a/src/QsFmt/App/DesignTimeBuild.fsi b/src/QsFmt/App/DesignTimeBuild.fsi index 4e598d73cc..9a59fc6cb5 100644 --- a/src/QsFmt/App/DesignTimeBuild.fsi +++ b/src/QsFmt/App/DesignTimeBuild.fsi @@ -4,8 +4,9 @@ module internal Microsoft.Quantum.QsFmt.App.DesignTimeBuild /// Given a path to a project file, returns the list of files associated - /// to the project and the Quantum SDK version if found. - val getSourceFiles : string -> string list * string option + /// to the project and the Quantum SDK version. + /// Errors if given a file that is not a project file using Microsoft.Quantum.Sdk. + val getSourceFiles : string -> string list * string /// Initializes the assembly load context needed for building project files. val assemblyLoadContextSetup : Unit -> Unit From 7223e7601748b8333f75db1fbc3037d56e0105d3 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 14 Oct 2021 23:53:30 -0700 Subject: [PATCH 35/35] fantomas --- src/QsFmt/App.Tests/Tests.fs | 8 +++++++- src/QsFmt/App/DesignTimeBuild.fs | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/QsFmt/App.Tests/Tests.fs b/src/QsFmt/App.Tests/Tests.fs index 14d8e99049..142c170ac2 100644 --- a/src/QsFmt/App.Tests/Tests.fs +++ b/src/QsFmt/App.Tests/Tests.fs @@ -360,4 +360,10 @@ let ``Outdated system project file as input`` () = let project = "Examples\\TestProjects\\OldApplication\\OldApplication.csproj" let act = Action(fun () -> run [| "update"; "-p"; project |] "" |> ignore) let ex = Assert.Throws(act) - Assert.Equal(ex.Message, sprintf "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." project) + + Assert.Equal( + ex.Message, + sprintf + "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." + project + ) diff --git a/src/QsFmt/App/DesignTimeBuild.fs b/src/QsFmt/App/DesignTimeBuild.fs index bbf063f4c2..63ceed11bf 100644 --- a/src/QsFmt/App/DesignTimeBuild.fs +++ b/src/QsFmt/App/DesignTimeBuild.fs @@ -54,7 +54,12 @@ let getSourceFiles (projectFile: string) = let version = match instance.Properties |> Seq.tryFind (fun x -> x.Name = "QuantumSdkVersion") with | Some v -> v.EvaluatedValue - | _ -> failwith (sprintf "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." projectFile) + | _ -> + failwith ( + sprintf + "The given project file, %s is not a Q# project file. Please ensure your project file uses the Microsoft.Quantum.Sdk." + projectFile + ) (sourceFiles, version)