I am working on a new extension for VSCode that uses a Virtual File System and displays a folder and files tree using the TreeView API. A TreeItem object represents a file or a folder and it can be customized to better represent the object it represents: folder for example can be expanded to show their content through collapsibleState, or a command to execute when the item is clicked in the TreeView (for example this allows to open a file in a new editor). It is also possible to customize a TreeItem appearance with an icon, very useful to visually identify item types: Here comes the lesson learned. Since my extensions deals with files and folders, I wanted them to respect the user’s theme so I set iconPath to the appropriate value from ThemeIcon Anyway, running the extension, presented me with this: All the TreeItem instances respect their assigned type (file or folder) but they are definitely not using my icons theme. It took me some back and forth before I noticed this in the TreeItem documentation: resourceUri The URI of the resource representing this item. Will be used to derive the label, when it is not provided. Will be…
-
-
What do you do for fun? A Visual Studio Code extension
The pandemic has come and gone (?), as most working for tech companies I worked exclusively from home for over two years and even if our offices have been technically open for about three months, I and most of my colleagues are still working from home at least a couple of days a week (and some, hired during the pandemic, have not even seen our offices yet). At the height of the pandemic, during summer of 2020 (before vaccines where available, most public places closed or with entrance restrictions, masks mandates for indoor activities… I’m sure you remember) I had a bit of free time in my hands and decided to learn something that could hopefully also be useful. I have been using Visual Studio Code since the very first release and always been curios about how to write and extension. I had already created a simple color theme (Hogwarts colors for VSCode) but this time I also wanted a relatively simple project to learn Typescript; I thought an extension to help working with text (conversions, manipulations, sorting, that kind of things) could be useful for working with log files and parsing command line output (I use those a lot).…
-
File is not `gofmt`-ed with `-s`
Quick tip today, hopefully to save you a headache: if in a Go project you get the error File is not gofmt-ed with -s (the error will likely show a file path), try to run this command to fix it: gofmt -w <file_path>. The file is likely not properly formatted according to Go rules, in my case I had accidentally used tab indentation rather than spaces… š£ PeopleĀ demandĀ freedom ofĀ speechĀ to make up for theĀ freedomĀ ofĀ thoughtĀ which they avoid. Socrates
-
Microsoft authentication provider is not available (VSCode settings sync) – AADSTS700003
I have been using settings sync in Visual Studio Code since it was first released in the Insiders build and it has been working well for me, I currently sync settings from Insiders and Stable releases on four different Windows machines and one MacBook Pro. Then, all of a sudden the Insiders instance on one of the Windows computers stopped syncing; Weirdly enough, the Stable build on the same machine kept happily working and syncing settings, suggesting the problem was specific to this Insiders installation and not some machine wide problem. It’s worth pointing out again installation, it was not even a problem with the general Insiders build since the same build was still working fine on all other machines. At first I tried to start VSCode without extensions (code-insiders --disable-extensions), then I tried to fully remove VSCode (and manually deleted all relevant folders under my local profile), restarted Windows… the usual deal, all with no luck. I then decided to open an issue on GitHub (more on that later) while I continued my investigation; the first comment from RMacfarlane suggested something might have changed with the network configuration on the machine, but again, that should have affected the Stable…
-
New role, spring cleaning and new modules
Some time ago my team was involved in a reorg (I’ll skip the boring details), the end result is that I am now working on a different (and new) service and I am transitioning to a new role. This seemed a good moment to do some spring cleaning and reorganize my tools/scripts/modules etc… Over the years I have built a good amount of powershell scripts and modules (among other tools), most of them targeted for my and my colleagues specific work and role but I wanted to see if there was some I could instead share to hopefully help someone else. It took some time, I ended up deleting a number of old scripts no longer relevant (well, I couldn’t really delete them, they are just archived in a private GitHub repo š) and I have reworked some of my modules and functions into a collection hopefully worth sharing. The result is three new modules you can download either from the Powershell Gallery or from GitHub (and contributions are welcome, should you feel generous enough to open issues or send me a pull request š). PSToolbox (GitHub, Powershell Gallery) is a collection of functions for every day operations, things like…
-
Reverse a string in powershell
Exactly a month in of working from home and social distancing, last night I was playing around with some scripts and at some point I needed to read the last index or an array without knowing its length. Easy enough, regardless the array length, the last index is always -1: This reminded me of a conversation I had some time ago with a colleague, he was trying to convince me of much much Python is better than Powershell (and I stoically resisted and counter-punched š). One of the many arguments he used to convince me was to show how easy it is in Python to revert a string: True, that looks easy and elegant but the Powershell solution is not that bad, is it? A string is an array of characters, so I’m using the square brackets to read a sequence of characters from the original string. The trick here is that my starting point is index -1 (the last character of the string) and I’m moving backwards towards the first character; here I’m just using a negative index greater than the string length and powershell happily complies. I also need to join the output (with an empty string). If…
-
Break the pipeline
foreach can be confusing if you don’t pay attention to the context. Googling (or Binging, is that even a word? š¤) around you can easily find articles and blog posts about foreach, how it is both a keyword and an alias (shortcut) for Foreach-Object and how foreach as keyword is different from Foreach-Object. Here are a couple of examples: PowerShell foreach loops and ForEach-Object Getting to know ForEach and Foreach-Object foreach as keyword is a pattern common across many programming and scripting languages used to loop through a list: foreach as alias for Foreach-Object can be used to get a similar output: In a more complex, real life scenario I may need to exit the loop without going through the whole list, that’s easily done using the break keyword: Ok, this is not a real, real world scenario but you get the idea: I’m looping through the list of files till a reach one named jobs.ps1 and skip the rest of the iteration, and as expected, the sample still prints the after loop message. Let’s try with Foreach-Object: Uhm… The loop was interrupted but at the wrong moment (jobs.ps1 should have not been printed to the console), moreover the output…
-
Test Powershell on Linux with Visual Studio Code and Docker
One of the promises (maybe, the biggest promise) of .NET Core and Powershell Core is being cross-platform, be able to develop applications (or Powershell scripts and modules) that can be executed on Windows, Linux and macOS; but how can we be sure the our application or script/module will actually run properly across all those platforms and distros? Talking about Powershell, PSScriptAnalyzer helps to check a script or function compatibility with certain Powershell versions or different platforms. But what if I want to actually write and test my script/module on Linux (assuming my main machine is Windows, of course š)? One easy solution is to spin up a virtual machine, install the tools I need (.NET Core, Powershell Core, Visual Studio Code and anything else required) or use the Remote Development extension pack for Visual Studio Code and use Docker containers instead. Note: this requires Visual Studio Code Insiders at this time; pay attention to the Installation notes for good tips to get started. Also, take a look at Developing Inside a Container in the official VSCode documentation. The extension pack allows to choose between Windows Substrate for Linux, connect to a remote machine through SSH or run a Docker container…
-
Powershell Core binary module with Visual Studio Code
I wrote countless scripts and a good amount of modules (and functions) in my years as Service Engineer but all of them are script modules, I never really created binary modules. The main reason is that I like to write in Powershell but I also like the fact that, not being a compiled language, it is very easy to share and modify the source code for a script module and it is immediately ready to be reloaded and used. Anyway out of curiosity and to learn a different approach to building modules, I decided to try to convert one of mine from script to binary; my first step was (of course) a quick search to find some samples and getting started articles and I found a few good ones (referenced below) but all of them use Visual Studio and the full version of the .NET Framework, while I want to use Visual Studio Code and .NET Core. So here’s what I came up with. First off of course I need .NET Core (I am using the latest .NET Core 3 preview 8 at the moment), Visual Studio Code and the C# Extension. Next, I’m going to create a Class Library…
-
Three modules and a video
While I took a longer break than I wanted to from blogging (busy days at work… š¤) I still managed to stumble across a number of interesting Powershell modules, they bring little improvements here and there in my workflow and smooths out wrinkles from some tasks that are not are painless are they are supposed to be. PSReleaseTools This module by Jeff Hicks makes it easy to stay up to date with the latest Powershell releases: get information about the release changes, download and install the new bits and so on. PSScriptTools Another module by Jeff Hicks, this one is a collection of tools for different occasions, there are functions to work with modules, create extended type files, create objects from text output, work with files and optimize the text content, improve and colorize the output to the powershell console and so on. Check it out, you’ll likely find something useful for your scenario. EnvironmentVariableCmdlet This is a module by my colleague James Truher to simplify working with environment variables; Jason Helmick has a funny Youtube video where he (among other things) gives an overview of this module. And in the end it’s not theĀ yearsĀ in yourĀ lifeĀ thatĀ count. It is the life…